本文介绍了如何在按钮点击选择div文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在点击按钮上选择div内容。
HTML
< div id =divid>你好这个div内容必须被选择。 < / DIV>
< button onclick =selectText(divid);>选择Div< /按钮>
JS
function selectText(divid){
if(document.selection){
var div = document.body.createTextRange();
div.moveToElementText(document.getElementById(divid));
div.select();
}
else {
var div = document.createRange();
div.setStartBefore(document.getElementById(divid));
div.setEndAfter(document.getElementById(divid));
window.getSelection()。addRange(div);
}
}
解决方案
试试下面的@Rajagopal Subramanian
function selectText(containerid){if(document.selection){var range = document.body.createTextRange(); range.moveToElementText(的document.getElementById(数据筒)); range.select(); } else if(window.getSelection){var range = document.createRange(); range.selectNode(的document.getElementById(数据筒)); 。window.getSelection()的AddRange(范围); }}
< div id =divid> < / div>< button onclick =selectText('divid')>选择< / button>
I want to select the div content on click button.
HTML
<div id="divid">Hello This div content have to be select. </div>
<button onclick="selectText(divid);"> Select Div</button>
JS
function selectText(divid) {
if (document.selection) {
var div = document.body.createTextRange();
div.moveToElementText(document.getElementById("divid"));
div.select();
}
else {
var div = document.createRange();
div.setStartBefore(document.getElementById("divid"));
div.setEndAfter(document.getElementById("divid"));
window.getSelection().addRange(div);
}
}
https://jsfiddle.net/rajagopalx/xds4y0en/
解决方案
Try the below @Rajagopal Subramanian
function selectText(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
}
}
<div id="divid">Hello This div content have to be select.</div>
<button onclick="selectText('divid')">Select</button>
这篇关于如何在按钮点击选择div文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!