Appending a link with Jquery

2019-06-22 08:38发布

问题:

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;
}