代码之家  ›  专栏  ›  技术社区  ›  Christoffer Corfield Aakre

无法使用jQuery选择元素

  •  1
  • Christoffer Corfield Aakre  · 技术社区  · 9 年前

    https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js 我想防止链接将用户带到另一个页面。 当我试着用getElementById(“id”)选择元素时,效果很好。然而,当我尝试使用$(“#id”)进行选择时,它不起作用。为了澄清,这是可行的: https://jsfiddle.net/1nwaptL9/

    https://jsfiddle.net/7vkepm9e/1/

    我意识到这两个小提琴实际上都能工作,但当我在Chrome中加载HTML文件时,带有jQuery选择的那个不能工作。由于我在下面包含的源代码在JSFiddle中运行良好,但在Chrome中运行不正常,我怀疑我做错了什么,但JSFiddle不处理链接或类似的东西。任何帮助都将不胜感激!

    不起作用的源代码:

    <html>
    <body>
    <a id="preventlink" href="http://youtube.com"> click here </a>
    <script src="js/jquery-1.11.3.min.js"> </script>
    </body>
    </html>
    
    <script>
    var link=$("#preventlink");
    link.addEventListener("click", function(e) {
        e.preventDefault()}, false)
    
        </script>
    
    5 回复  |  直到 9 年前
        1
  •  5
  •   KevBot    9 年前

    jQuery创建一个jQuery实例,如果需要实际的DOM元素,可以执行以下操作:

    var link = $("#preventlink")[0];
    

    或者,继续使用jQuery添加事件:

    $("#preventlink")
        .click(function (e) {
            e.preventDefault();
        });
    

    var link = $("#preventlink")[0];
    link.addEventListener("click", function(e) {
      e.preventDefault()
    }, false);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a id="preventlink" href="http://youtube.com"> click here </a>

    坚持使用jQuery:

    $("#preventlink").click(function(e) {
      e.preventDefault()
    });
    <脚本src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“>lt;/script>

    看见 jQuery's doc 关于这个主题

        2
  •  3
  •   Pooja Chauhan    9 年前

    这是因为addEventListener是一个javascript函数。它不是为jquery定义的。对于jquery,可以使用bind函数。下面是示例代码

    $(document).ready(function() {
    var link=$("#preventlink");
     link.bind('click', function(e){
      
        e.preventDefault();})
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <html>
    <body>
    <a id="preventlink" href="http://youtube.com"> click here </a>
    
    </body>
    </html>
        3
  •  2
  •   Syden    9 年前

    您将jQuery与vanilla JS混合使用:

    $("#id") jQuery

    document.getElementById("id") --

    试着使用其中一个。

    $("#preventlink").on("click", function(e) {
      e.preventDefault()
    })
    

      var link=document.getElementById("preventlink");
      link.addEventListener("click", function(e) {
        e.preventDefault()}, false)
    
        4
  •  1
  •   ihpar    9 年前

    这里有一种更具jQuery风格的方式来实现您想要的功能

    var $link = $("#preventlink");
    
    $link.on("click", function(e) {
      e.preventDefault();
      alert("link clicked");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <html>
    	<body>
    		<a id="preventlink" href="http://youtube.com"> click here </a>
    		<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
    	</body>
    </html>
        5
  •  1
  •   Sam Hanley    9 年前

    $("#preventlink").on('click', function (e)
    { 
        e.preventDefault (); 
    });