我想在单击或双击 <p>
时选择段落中的文本标签。不高亮,就像用鼠标做一个选择区域来选择要选择的文本!
我在页面上有好几个段落和*.rar文件链接地址,点击其中一个想全选文字。我认为文本框可以那样工作,但我喜欢它位于段落或链接标记中。
有没有办法通过单击另一个元素来选择段落中的所有文本?
最佳答案
这是一个函数,它将选择您传递给它的元素的内容:
function selectElementContents(el) {
var range;
if (window.getSelection && document.createRange) {
range = document.createRange();
var sel = window.getSelection();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
} else if (document.body && document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(el);
range.select();
}
}
window.onload = function() {
var el = document.getElementById("your_para_id");
selectElementContents(el);
};
关于javascript - 单击文本时选择文本就像 "Ctrl+A"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4011490/