听起来您正在寻找一些 jQuery,它将点击事件绑定到所有<p>
标签并将所有未点击标签的 CSS 更改为user-select: none;
. 释放鼠标后,将所有<p>
标签恢复正常user-select: all;
我的代码是资源密集型的,因此您可能希望根据自己的喜好细化选择器。
JSFiddle
HTML
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis partu
</p>
<p>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam
</p>
<p>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam
</p>
CSS
.select_off{
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
-ms-user-select: none;
user-select: none;
}
JS
$(document).ready(function(){
// Mouse was clicked down
$(document).on('mousedown', 'p', function(){
// Give all <p> tags an ID if they do not have one
var i = 1;
$('p').each(function(){
if(!$(this).attr('id')){
$(this).attr({'id':'unique_'+i+'id'});
}
i += 1;
});
var p_clicked = $(this);
var p_clicked_id = p_clicked.attr('id');
// Loop through <p> tags and add the .select_off class
$('p').each(function(){
if($(this).attr('id') != p_clicked_id){
$(this).addClass('select_off');
}
});
});
// Mouse was released
$(document).on('mouseup', 'p', function(){
// Loop through <p> tags and remove the .select_off class
$('p').each(function(){
$(this).removeClass('select_off');
});
});
});