-3

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

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>

我想要一个在单击哈希锚或 url 锚时发出警报的代码?

像这样的东西: -

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") }

我提到了这个但找不到确切的解决方案?

4

1 回答 1

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");
  }
});
于 2013-09-02T09:30:30.153 回答