如何在没有href的情况下检测链接标记?

时间:2014-07-22 03:32:24

标签: jquery

奇怪的是,有些人喜欢在没有hrefs的情况下创建<a>标签,而且这些标签会绊倒我项目的某个部分。以下是我到目前为止的情况:

        iframe.find("a").not("a[href*='#'], a[href*='%'], a[href*='javascript:;']").bind("click", function() {

            NProgress.start();

            $("button.active .tab-title").html(currentTitle);
            $("button.active .tab-favicon").attr("src", getFavicon);

            var _location = $(this)[0].href;
            $("iframe.active").attr("src", _location);

            console.log("Hmm, " + _location);

        });

我有一个进度条,显示点击链接的加载。当然,如果没有href / source,它就会窒息。如何检测没有来源的<a>?将a添加到.not部分并不起作用,因为我的加载栏无法正常工作。

4 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

    if ($(this).attr("href").length == 0) {
       alert("no href for this link");
       return false;
    }

或者如果您只想定位href

的链接
if ($(this).attr("href").length > 0) {  }

答案 1 :(得分:0)

使用jquery并检查属性是否为空:

 if ($(this).attr('href') != '') { // if no href }

答案 2 :(得分:0)

您可以查看this.href。如果属性为空或不存在,它将是一个空字符串。

答案 3 :(得分:0)

var hrefattr = $(this).attr("href");
if(hrefattr != ' '){
    //your code here
}

仅使用href获取tag的值并存储在变量中,如果它不为空,则表示您已获得带有href属性的标记。

相关问题