如果锚有hashtag / url / mail / phone,则发出警报

时间:2013-09-02 09:26:22

标签: javascript jquery html hash

就这么简单,我正在使用带有哈希标签和网址的菜单栏......

HTML: -

<ul class="nav">
           <li class="active"><a href="#firstpage">home</a></li>
                <li><a href="demo.html">Demo</a></li>
                <li><a href="#secondpage">second</a></li>
                <li><a href="#thirdpage">third</a></li>
                <li><a href="#fourthpage">fourth</a></li>
                <li><a href="">fifth</a></li>
                <li><a href="mailto:abc@xyz.com">Email</a></li>
                <li><a href="tel:+000">Phone</a></li>
                <li><a href="#">last</a></li>
            </ul>

我希望有一个代码可以在点击哈希锚点或网址锚点时发出警告?

这样的事情: -

if("anchor has a hash") { alert("It is a hash"); }
else if("anchor has a url") { alert("It is a url"); }
else if("anchor has a email") { alert("It is a email"); }
else if("anchor has a phone number") { alert("It is a phone number"); }
else { alert("Its empty") }

我所提到的this但是找不到确切的解决方案?

1 个答案:

答案 0 :(得分:2)

这应该有帮助

$(".nav a").on("click", function(event) {
  var href = $(this).attr("href");

  if (href.indexOf("#") !== -1) {
    alert("hashtag");
  }
  else if (href.length > 0) {
    alert("has a url");
  }
  else {
    alert("blank");
  }
});