一次单击即可选择所有DIV文本

一次单击即可选择所有DIV文本,第1张

一次单击即可选择所有DIV文本
function selectText(containerid) {    if (document.selection) { // IE        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().removeAllRanges();        window.getSelection().addRange(range);    }}<div id="selectable" onclick="selectText('selectable')">http://example.com/page.htm</div>

现在,您必须将ID作为参数传递,在这种情况下该ID是“可选的”,但它更具全局性,允许您多次在任何地方使用它,而不必像chiborg所提到的那样使用jQuery。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5009197.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存