代码之家  ›  专栏  ›  技术社区  ›  dennismonsewicz

特定文本的jQuery搜索ID

  •  0
  • dennismonsewicz  · 技术社区  · 14 年前

    我正在寻找在当前单击的元素上搜索ID的方法。

    $('.t_element').click(function(){
                if(!$('.t_element [id*=meta]')) {
                    transModal($(this));
                    translateText();
                }
            });
    

    我基本上需要说,如果单击的元素id不包含单词“meta”,那么继续脚本,但这不起作用。

    对不起,如果这是混淆。

    谢谢! 丹尼斯

    if (!$(this).is('[id*=meta]')) {
        transModal($(this));
        translateText();
    }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   lonesomeday    14 年前

    萨弗雷斯的版本应该可以。另一种方法是 .is() :

    if ($(this).is('[id*=meta]')) {
        transModal($(this));
        translateText();
    }
    

    或者,就像帕特里克说的,如果你想 如果元素具有包含“meta”的id,则执行以下操作:

    if ($(this).not('[id*=meta]')) {
        transModal($(this));
        translateText();
    }
    
        2
  •  2
  •   user113716    14 年前

    .click() 处理程序。

    $('.t_element:not([id*=meta])').click(function(){
        transModal($(this));
        translateText();
    });
    

    这使用 the :not() selector 随着 the attribute contains selector meta .

        3
  •  1
  •   Sarfraz    14 年前

    试一试 length

    $('.t_element').click(function(){
       if($('[id*="meta"]', $(this)).length === 0) {
         transModal($(this));
         translateText();
       }
    });
    

    或:

    $('.t_element').click(function(){
       if($(this).attr('id').indexOf('meta') <= -1) {
         transModal($(this));
         translateText();
       }
    });