如果在元素外部单击,则删除类

时间:2016-06-22 21:18:03

标签: javascript jquery html css

如何点击paragraph元素,然后删除背景颜色?注意我希望它一次只能突出显示一个p



$('p').click(function() {
    $('p').removeClass('yellow');
    $(this).addClass('yellow');
});

.yellow {
    background: yellow;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit tenetur sequi amet sit dolorem, nulla inventore quo cum ad distinctio aut nesciunt reprehenderit dolorum quidem animi unde aspernatur. Esse, eius!</p>
            <p>Deleniti vitae rerum eum saepe eaque tenetur libero, omnis nisi sapiente dicta est repellat, provident placeat quia inventore, at architecto quisquam, pariatur minus quam magni totam praesentium dignissimos. Incidunt, sequi.</p>
            <p>Fuga cupiditate consectetur, corporis architecto, doloremque impedit ullam quia praesentium voluptatibus molestiae dolor sint, odio amet atque culpa fugit blanditiis ea nam repellat necessitatibus. Aliquam voluptate fuga quo, omnis mollitia.</p>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

这是你想要的:

$('p').click(function(e) { 

$('p').removeClass('yellow'); //If there is any p with class yellow, it removes that
  $(e.target).closest('p').addClass('yellow'); //This find closest target when you click, in our case current paragraph(p)

});
$(document).click(function(e){ //Here is when you click in your entire document
  if($(e.target).closest('p').length==0) { // If click is not paragraph
     $('p').removeClass('yellow');  //It removes this class existed from any paragraph
    }
  

})
.yellow {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-md-12"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit tenetur sequi amet sit dolorem, nulla inventore quo cum ad distinctio aut nesciunt reprehenderit dolorum quidem animi unde aspernatur. Esse, eius!</p>
<p>Deleniti vitae rerum eum saepe eaque tenetur libero, omnis nisi sapiente dicta est repellat, provident placeat quia inventore, at architecto quisquam, pariatur minus quam magni totam praesentium dignissimos. Incidunt, sequi.</p>
<p>Fuga cupiditate consectetur, corporis architecto, doloremque impedit ullam quia praesentium voluptatibus molestiae dolor sint, odio amet atque culpa fugit blanditiis ea nam repellat necessitatibus. Aliquam voluptate fuga quo, omnis mollitia.</p></div>
  </div>
</div>

答案 1 :(得分:1)

使用此代码

$("p").click(function (e){
    $("p").removeClass("yellow");
    $(this).addClass("yellow");
});

$(document).click(function (e){
    var element = $("p.yellow");

    if (!element.is(e.target) && element.has(e.target).length === 0)
        element.removeClass("yellow");
});
.yellow {
    background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="yellow">Text text text</p>
<p>Text text text</p>
<p>Text text text</p>

答案 2 :(得分:0)

您可以将监听器设置为<body>并检查点击是否在<p>上:

$('body').click(function(event) {
    if(!$(event.target).is('p')) {
        $('p').removeClass('yellow');
    }
    else {
        $(event.target).addClass('yellow');
    }
});