javascript - 如何在点击链接时弹出提醒

标签 javascript jquery html alert

我是 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/

相关文章:

jquery - 向 Accordion 标题 (h3) 标签添加新元素以及如何在标题中设置新元素的样式

jquery - setTimeout() 函数在 jQueryeach() 函数内部不起作用

javascript - 使用 jQuery 到 'click' 一个 li 元素

javascript - 我怎么能有一个 iframe 以 50% 的比例显示目标页面及其所有元素?

javascript - 我怎么知道我的 Javascript 执行了?

javascript - 使用 JQuery 保存每个变量的计数

javascript - 如何使用 JavaScript 禁用输入/单击文本框/图像?

javascript - 使用谷歌地图 API 时出现错误 : This API project is not authorized to use this API

javascript - polymer 中的 Firebase 共享身份验证

javascript - 如何在一个请求中上传多个文件?