我是 html 和 jquery 的新手。我想在单击链接时显示一个警报框。但 jquery 函数似乎不起作用。 不确定选择器或其他错误。
请提出建议。谢谢。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<p>text1</p>
<p>text2</p>
<p>text3</p>
<input type=button onClick="location.href='http://www.test.com/?test=123'" value='click here'>
<div class="modify">
<a href="http://www.test.com" id="myHref">test1</a>
<p/>
<a href="http://www.test.com" id="myHref">test2</a>
<p/>
<a href="#" id="myHref1" class="button mini-button blue">test3</a>
</div>
<script type="text/javascript">
function ($) {
alert('test');
}
$self.find("#myHref").each(function () {
$(this).on("click", function () {
var $this = $(this),
alert ($this.attr('href'));
});
});
</script>
<body>
</html>
最佳答案
这是您更新后的代码
function n($) {
alert('test');
}
n()
$(document).find("#myHref").each(function () {
$(this).on("click", function () {
var $this = $(this);
alert ($this.attr('href'));
return false;
});
});
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<html>
<body>
<p>text1</p>
<p>text2</p>
<p>text3</p>
<input type=button onClick="location.href='http://www.test.com/?test=123'" value='click here'>
<div class="modify">
<a href="http://www.test.com" id="myHref">
test1
</a>
<p/>
<a href="http://www.test.com" id="myHref">
test2
</a>
<p/>
<a href="#" id="myHref1" class="button mini-button blue">
test3
</a>
</div>
<body>
关于javascript - 如何在点击链接时弹出提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31862535/