我想添加一个按钮,使用javascript选择/取消选择div中的文字。
在示例中,单击一下将选择文本,下一次单击将取消选择文本,依此类推。
答案 0 :(得分:0)
如果您想选择/取消选择,只需将点击处理程序调整为以下内容:
<span rel="theDiv">select text of div</span>
<div id="theDiv">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</div>
$('span').click(function() {
var targetDiv = $(this).toggleClass("selected").attr("rel");
if($(this).hasClass("selected")){
SelectText(targetDiv);
}
});
jsfiddle上的示例。