选择与& quot; Ctrl + A& quot;类似的文本单击文本时? [英] Select text just like "Ctrl+A" when clicking the text?

查看:55
本文介绍了选择与& quot; Ctrl + A& quot;类似的文本单击文本时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击或双击< p> 标记时选择段落中的文本.不突出显示,就像使用鼠标在选择区域中选择要选择的文本一样!

I want to select the text in a paragraph when I click or double click the <p> tag. Not highlight, just like using mouse to make a select area to choose text to be selected!

我在页面上有几个段落和* .rar文件链接地址,当我单击其中一个时,我想选择所有文本.我认为文本框可以那样工作,但我喜欢将其放在段落或链接标记中.

I have several paragraph and *.rar file link addresses on the page, and I want to select all the text when I click on one of them. I think the textbox could work that way but I like it to be in a paragraph or link tag.

是否可以通过单击另一个元素来选择段落中的所有文本?

Is there a way to select all text in paragraph by clicking another element?

推荐答案

以下是一个函数,它将选择传递给它的元素的内容:

Here's a function that will select the contents of the element you pass to it:

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);
};

这篇关于选择与&amp; quot; Ctrl + A&amp; quot;类似的文本单击文本时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆