English 中文(简体)
如果存在联系,则使用j Query进行检查
原标题:Use jQuery to Check if Link exist

我拥有一个包含以下链接的图谋:previous page <span id=“previous”><a href=“www.site.com/page/1” >Previous</a>;n>ext/code 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");
                    }
                });

现在的问题是,似乎存在着<代码>#pagination_previous,点击图像仍向用户转移。 在这种情况下,在第1页,用户被转至un specified

How can I solve 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





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签