admin管理员组文章数量:1332403
I have a pagination div that contains links to the previous
page <span id="previous"><a href="www.site/page/1" >Previous</a>
, and to the next
page. On the first page, there will not be any link to the previous page.
Now I have 2 image buttons with front and back arrows. If the user clicks on these buttons, jQuery will take the link from the pagination div as mentioned above and redirect the user to these links. However if the pagination link does not exist like on the first page, clicking the previous button
will not do anything.
My Code:
//img has id = info_rightclick_left
$("#info_rightclick_left").click(function() {
if($("#pagination_previous")) {
window.location = $("#pagination_previous a").attr("href");
} else {
alert("NOO");
}
});
The problem now is that it seems like whether or not #pagination_previous
exists, clicking the image still redirects the user. In this case, on page 1, the user gets redirected to undefined
How can I solve this?
I have a pagination div that contains links to the previous
page <span id="previous"><a href="www.site./page/1" >Previous</a>
, and to the next
page. On the first page, there will not be any link to the previous page.
Now I have 2 image buttons with front and back arrows. If the user clicks on these buttons, jQuery will take the link from the pagination div as mentioned above and redirect the user to these links. However if the pagination link does not exist like on the first page, clicking the previous button
will not do anything.
My Code:
//img has id = info_rightclick_left
$("#info_rightclick_left").click(function() {
if($("#pagination_previous")) {
window.location = $("#pagination_previous a").attr("href");
} else {
alert("NOO");
}
});
The problem now is that it seems like whether or not #pagination_previous
exists, clicking the image still redirects the user. In this case, on page 1, the user gets redirected to undefined
How can I solve this?
Share asked Jul 5, 2011 at 15:45 thedethethedethe 872 silver badges4 bronze badges3 Answers
Reset to default 4Try this.
$("#info_rightclick_left").click(function() {
if($("#pagination_previous").length) {
window.location = $("#pagination_previous a").attr("href");
} else {
alert("NOO");
}
});
the answer in your question itself! just check for undefined like so:
$("#info_rightclick_left").click(function() {
if($("#pagination_previous a").attr("href") !== "undefined") {
window.location = $("#pagination_previous a").attr("href");
}
});
if($("#pagination_previous").length > 0) {
jQuery returns a list of captured elements, if it doesn't find any it will return an empty list, but still a list
本文标签: javascriptUse jQuery to Check if Link existStack Overflow
版权声明:本文标题:javascript - Use jQuery to Check if Link exist - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742231884a2437368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论