使用 Jquery 附加链接 [英] Appending a link with Jquery
问题描述
我正在尝试根据您所在的页面添加指向我的页面的链接.我正在使用 Squarespace 来构建这个网站,所以对我来说,最简单的方法是使用 Javascript 或 Jquery.
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> ")
}
}
}
我刚刚检查过,链接似乎在 Chrome 中有效,但在 Firefox 中无效.
I just checked, and the links seem to be working in Chrome, but not Firefox.
另一个所以该链接在 Safari 中也有效.我想现在的问题是为什么不在 Firefox 中?Firefox 不支持此方法吗?
Another 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?
推荐答案
您的代码似乎运行良好,但是您可以使用 else if
语句代替嵌套的 if
语句:
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> ")
}
这是一个演示:http://jsfiddle.net/Rba6w/
这篇关于使用 Jquery 附加链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!