我试图阻止我网站上的某些链接停止工作。
这是我的代码:
链接:
<a href='http://mysite.home.com'>Home</a><br>
<a href='http://mysite.home.com/nextpage'>Next Page</a><br>
<a href='http://mysite.string.home.com'>Home 2</a><br>
<a href='http://mysite.string.home.com/otherpage'>Other Page</a>
Jquery的:
<script>
$("a[href*='.string.']").click(function(e) {
e.preventDefault();
alert('You cannot use this link');
});
</script>
目标是停止最后两个链接(href包含'.string。'),而不是显示警报。但是,此代码不起作用,链接仍然有效。我做错了什么?
奇怪的是代码在这里工作正常:http://jsfiddle.net/TzUN3/364/
但不在我的网站上。我在我的页面的<body>
,链接后的脚本中都有脚本和链接。这是对的吗?
感谢。
答案 0 :(得分:1)
忘记添加
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
到我的页面。感觉像个白痴。
谢谢大家。
答案 1 :(得分:0)
$(function() {
// Jquery is ready
// *= is contains
$("a[href*='.string.']").click(function(e) {
e.preventDefault();
alert('You cannot use this link');
});
});