我想在点击按钮上选择div内容。
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);
}
}
答案 0 :(得分:13)
尝试以下@Rajagopal Subramanian
<div id="divid">Hello This div content have to be select.</div>
<button onclick="selectText('divid')">Select</button>
&#13;
<asp:RequiredFieldValidator ID="rfvPropertyName"
Text="*"
CssClass="RedText"
runat="server"
ErrorMessage="please enter your name."
ValidationGroup="QuerySystem"
ControlToValidate="txtPropertyName" Enabled="False"
></asp:RequiredFieldValidator>
&#13;
答案 1 :(得分:1)
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);
}
}
答案 2 :(得分:-3)
我希望它会对你有所帮助。
html代码
<div id="divid">Hello This div content have to be select. </div>
<button id="hit"> Select Div</button>
<强> demo.js 强>
$(document).ready(function(){
$('#hit').on('click', function(){
var sav = $('#divid').html();
alert(sav);
});
});