我正在将数据从 django 后端传递到我的浏览器,该后端可能包含 {{ string }}
的空格,例如 This is the sentence
或 H3ll0W0 !rd
。双击时,我希望能够选择整个句子或字符串,以便可以复制。
HTML 的例子是
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Book Title</th>
<th>Book Code</th>
</tr>
</thead>
<tbody>
{% for b in books %}
<tr>
<td>{{ b.title }}</td>
<td>{{ b.code}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
最佳答案
您可以像这样使用双击监听器:
HTML:
<p id="test">Here is some text</p>
Javascript:
document.getElementById('test').ondblclick = function(){
event.preventDefault();
var sel = window.getSelection();
var range = document.createRange();
range.selectNodeContents(this);
sel.removeAllRanges();
sel.addRange(range);
};
关于javascript - 双击选择整个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33738795/