Appending a link with Jquery

拈花ヽ惹草 提交于 2020-01-03 12:59:07

问题


I'm trying to add a link to my page depending on what page you're already on. I'm using Squarespace to build this site, so the easiest way for me to do this would be with Javascript or Jquery.

I think there is something wrong with this syntax that i'm missing. I already tried to break out of the quotes with a \, but that wasn't working. If I'm just outputting text, it works fine, but it seems to break when I try and make it work with a link.

 if(loc == pageOne) {
$("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
    }else{
  if(loc == pageOneB){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
  }else{
    if(loc == pageTwo){
     $("#pages").append("<div> <a href=\"http://design.optimus.com/projects\">Previous Page</a></div> ")
  }
 }
}

Edit: I just checked, and the links seem to be working in Chrome, but not Firefox.

Another Edit: So the link is working in Safari too. I guess the issue now is why not in Firefox? Is there something about this method that Firefox doesn't support?


回答1:


Your code seems to work fine however you can use else if statements instead of nested if statements:

if(loc == pageOne) {
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
} else if (loc == pageOneB){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
} else if (loc == pageTwo){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects\">Previous Page</a></div> ")
}​

Here is a demo: http://jsfiddle.net/Rba6w/




回答2:


see if this works

if(loc == pageOne) {
$("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>')
    }else{
  if(loc == pageOneB){
    $("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>')
  }else{
    if(loc == pageTwo){
     $("#pages").append('<div> <a href="http://design.optimus.com/projects">Previous Page</a></div>')
  }
 }
}



回答3:


Try this out:

switch(loc)
{
case pageOne:
  $("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>');
  break;
case pageOneB:
  $("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>');
  break;
case pageTwo:
  $("#pages").append('<div> <a href="http://design.optimus.com/projects">Previous Page</a></div>"');
  break;
}


来源:https://stackoverflow.com/questions/9421032/appending-a-link-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!