选择文字就像“Ctrl + A”一样点击文字时? [英] Select text just like "Ctrl+A" when clicking the text?

查看:77
本文介绍了选择文字就像“Ctrl + A”一样点击文字时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击或双击< 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);
};

这篇关于选择文字就像“Ctrl + A”一样点击文字时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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