[英]how to pop up the alert when click the link
我是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>
$("body").on("click",".modify a", function() {
alert("hi");
});
因為您要在問題中引用JQuery
,所以可以直接使用帶有鏈接的click
事件來享受魔力:
$(function(){ //ready function
$('#myHref').click(function(){ //click event
alert($(this).attr('href'));
});
})
希望這就是你想要的。
您可以在href很簡單的情況下使用onclick:
<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">
<p>
<a href="http://www.test.com" id="myHref" onclick="return confirm('Are you sure?')">test1</a>
</p>
<p>
<a href="http://www.test.com" id="myHref" onclick="return confirm('Are you sure?')">test2</a>
</p>
<p>
<a href="http://www.test.com" id="myHref1" class="button mini-button blue" onclick="return confirm('Are you sure?')">test3</a>
</p>
</div>
</body>
</html>
並記住始終打開和關閉標簽。 例如:
<body>
<div>
<p>
</p>
</div>
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.