繁体   English   中英

单击链接时如何弹出警报

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM