简体   繁体   English

单击链接时如何弹出警报

[英]how to pop up the alert when click the link

i am newbie with html and jquery. 我是html和jquery的新手。 i want to show an alert box when click the link. 我想在单击链接时显示一个警告框。 but seems the jquery function doesn't work. 但似乎jquery函数不起作用。 not sure the selector or something else is wrong. 不确定选择器或其他错误。

please suggest. 请提出建议。 thanks. 谢谢。

<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>

Here is your updated code 这是您更新的代码

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

Because you're referencing JQuery in your question you can enjoy the magic using the click event directly with your links : 因为您要在问题中引用JQuery ,所以可以直接使用带有链接的click事件来享受魔力:

$(function(){ //ready function
    $('#myHref').click(function(){ //click event
         alert($(this).attr('href'));
    });
})

Hope that this what you want. 希望这就是你想要的。

you can use onclick in event of href just simple: 您可以在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>

And remember always open and /close tags. 并记住始终打开和关闭标签。 eg: 例如:

<body>
   <div>
       <p>
       </p>
   </div>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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