【问题标题】:jQuery selector to target elements with possible text content?jQuery选择器以可能的文本内容为目标元素?
【发布时间】:2011-10-30 00:55:08
【问题描述】:

我希望在任何具有或可以具有(或传统上具有)文本内容的“可编辑”类元素上触发点击事件:a、li、h1、h2、h3、p.. 但不是视频、img以及其他不直接包含文本的元素。

有没有办法在不引用特定 html 元素的情况下做到这一点?或者..我将如何从以下内容中排除特定元素类型:

$(".editable").click(function(event) {
   alert("edit text!");
});

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    我怀疑是否有一个只选择这些元素的预定义选择器。
    至于排除,你可以使用 not() 函数:

    $(".editable").not("video").not("img")
     .click(function(event) {
       alert("edit text!");
    });
    

    这是文档: http://api.jquery.com/not/

    编辑:调用一次函数可能会更有效:

    $(".editable").not("video,img")
     .click(function(event) {
       alert("edit text!");
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用 not 选择器或函数排除。

      选择器

      $('.editable:not(video, img)').click(function(e){
      
      });
      

      功能

      $('.editable').not('video, img').click(function(e){
      
      });
      

      JSFiddle with Example

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多