Jquery :contains showing alert for all span

前端 未结 4 1353
旧时难觅i
旧时难觅i 2021-01-14 16:10

I have a span Text goes her like this

$(\'#contentarea\').bind(\'click\',function(e){
             


        
4条回答
  •  忘掉有多难
    2021-01-14 16:34

    First of all a heads up, the :contains() selector is for the content of the DOM node, so you could use it with the following HTML:

    Hello World // will alert when clicked Goodbye World // will not alert when clicked

    And the following jQuery:

    $('#contentarea').bind('click',function(e){
        e.preventDefault();
        if($(e.target).is('span')){
            if($(e.target).is(':contains(Hello)')){
                alert($(e.target).text());
            }
        }
    });
    

    What you're trying to do is look at the applied CSS styles, or an attribute, so you would want to do something more like this:

    HTML:

    Hello World Hello World Hello World

    jQuery:

    $('#contentarea').bind('click',function(e){
        e.preventDefault();
        if($(e.target).is('span')){
            if($(e.target).css('font-weight')==="bold" || $(e.target).css('font-weight')==="700"){
                alert($(e.target).text());
            }
        }
    });
    

提交回复
热议问题