我可以使用除了textarea和输入文本元素之外的Javascript来突出显示任何文本吗?

时间:2022-05-25 06:39:52

using Javascript, it is easy to programatically select the text inside a textarea or input text element. How about for

使用Javascript,很容易以编程方式选择textarea或输入文本元素中的文本。怎么样

<span>The quick brown fox jumps over the lazy dog</span>

is it possible to use JavaScript to select the words "quick brown fox"? Or select the whole sentence?

是否可以使用JavaScript来选择单词“quick brown fox”?还是选择整个句子?

4 个解决方案

#1


Courtesy of http://www.sitepoint.com/forums/showthread.php?t=459934

由http://www.sitepoint.com/forums/showthread.php?t=459934提供

<script type="text/javascript">
    function fnSelect(objId) {
        fnDeSelect();
        if (document.selection) {
        var range = document.body.createTextRange();
            range.moveToElementText(document.getElementById(objId));
        range.select();
        }
        else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(objId));
        window.getSelection().addRange(range);
        }
    }

    function fnDeSelect() {
        if (document.selection) document.selection.empty(); 
        else if (window.getSelection)
                window.getSelection().removeAllRanges();
    }
    </script>
<body>

<div id="test1">
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
</div>
<div id="test2">
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
</div>
<a href="javascript:fnSelect('test1');">Select 1</a>
<a href="javascript:fnSelect('test2');">Select 2</a>
<a href="javascript:fnDeSelect();">DeSelect</a>
</body>

#2


You will need some way to manipulate the span tag. Such as an id, but then in javascript I would edit some of the style properties. You could have a property for everything you want to highlight like what is shown below.

您需要一些方法来操纵span标记。比如一个id,但是在javascript中我会编辑一些样式属性。您可以拥有要突出显示的所有内容的属性,如下所示。

.HL {
    background: #ffff99; 
    color: #000000;
} 

If you do that then you will need to get a reference to the specific tag.

如果您这样做,那么您将需要获得对特定标记的引用。

DocumentgetElementsByTagName("title")

Otherwise Document.getElementByID("ID") is good for unique ids. Then using setAttribute(name, value) To change the class to the one given.

否则,Document.getElementByID(“ID”)适用于唯一ID。然后使用setAttribute(name,value)将类更改为给定的类。

This is just a simple example but there are many ways to do this.

这只是一个简单的例子,但有很多方法可以做到这一点。

<span id="abc" class=""> Sample </span>
var temp = Document.getelementByID("abc");
temp.setAttribute('class', 'HL');

#3


in css you could create a .highlight class

在css中你可以创建一个.highlight类

.highlight{
  background-color:yellow;
}

in javascript you could add the .highlight class to the elements you wish to highlight

在javascript中,您可以将.highlight类添加到要突出显示的元素中

<getSpanElement>.className='highlight';

this text is then highlighted, though it is not selected.

然后突出显示此文本,但未选中。

#4


Highlight or select? I think you actually want to select not to highlight.

突出显示还是选择?我想你真的想选择不突出。

Highlight... yes! Set the proper CSS properties of the span element, for instance foreground and background color.

突出......是的!设置span元素的正确CSS属性,例如前景色和背景色。

Edit: That is called selecting. Sorry, I don't know of any way to do it.

编辑:这称为选择。对不起,我不知道有什么方法可以做到。

#1


Courtesy of http://www.sitepoint.com/forums/showthread.php?t=459934

由http://www.sitepoint.com/forums/showthread.php?t=459934提供

<script type="text/javascript">
    function fnSelect(objId) {
        fnDeSelect();
        if (document.selection) {
        var range = document.body.createTextRange();
            range.moveToElementText(document.getElementById(objId));
        range.select();
        }
        else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(objId));
        window.getSelection().addRange(range);
        }
    }

    function fnDeSelect() {
        if (document.selection) document.selection.empty(); 
        else if (window.getSelection)
                window.getSelection().removeAllRanges();
    }
    </script>
<body>

<div id="test1">
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
</div>
<div id="test2">
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
    <p>jhsdgfhlsdlfkjsdklgjs</p>
</div>
<a href="javascript:fnSelect('test1');">Select 1</a>
<a href="javascript:fnSelect('test2');">Select 2</a>
<a href="javascript:fnDeSelect();">DeSelect</a>
</body>

#2


You will need some way to manipulate the span tag. Such as an id, but then in javascript I would edit some of the style properties. You could have a property for everything you want to highlight like what is shown below.

您需要一些方法来操纵span标记。比如一个id,但是在javascript中我会编辑一些样式属性。您可以拥有要突出显示的所有内容的属性,如下所示。

.HL {
    background: #ffff99; 
    color: #000000;
} 

If you do that then you will need to get a reference to the specific tag.

如果您这样做,那么您将需要获得对特定标记的引用。

DocumentgetElementsByTagName("title")

Otherwise Document.getElementByID("ID") is good for unique ids. Then using setAttribute(name, value) To change the class to the one given.

否则,Document.getElementByID(“ID”)适用于唯一ID。然后使用setAttribute(name,value)将类更改为给定的类。

This is just a simple example but there are many ways to do this.

这只是一个简单的例子,但有很多方法可以做到这一点。

<span id="abc" class=""> Sample </span>
var temp = Document.getelementByID("abc");
temp.setAttribute('class', 'HL');

#3


in css you could create a .highlight class

在css中你可以创建一个.highlight类

.highlight{
  background-color:yellow;
}

in javascript you could add the .highlight class to the elements you wish to highlight

在javascript中,您可以将.highlight类添加到要突出显示的元素中

<getSpanElement>.className='highlight';

this text is then highlighted, though it is not selected.

然后突出显示此文本,但未选中。

#4


Highlight or select? I think you actually want to select not to highlight.

突出显示还是选择?我想你真的想选择不突出。

Highlight... yes! Set the proper CSS properties of the span element, for instance foreground and background color.

突出......是的!设置span元素的正确CSS属性,例如前景色和背景色。

Edit: That is called selecting. Sorry, I don't know of any way to do it.

编辑:这称为选择。对不起,我不知道有什么方法可以做到。