【问题标题】:how do i highlight ONLY the word i clicked on?如何仅突出显示我单击的单词?
【发布时间】:2014-07-21 10:02:24
【问题描述】:

假设我的页面有以下内容:

<p>This is the first sentence</p>
<p>This is the 2nd sentence.  This is the third.</>

如果我双击“This is the first sentence”中的“This”,我想突出显示(将其背景颜色更改为黄色)“This”一词的出现

如果我双击“This is the 2nd sentence”中的“This”,我想突出显示它并继续显示任何已经创建的突出显示。

如果我在“This is the first sentence”中双击“This”在突出显示后我想将其背景颜色更改回原来的颜色并继续显示任何已经创建的突出显示。

如果我双击“第三个”。我要突出显示“第三”

我已经看到了很多关于如何使用 jQuery 执行此操作的示例,但它们最终会突出显示所选单词的所有出现。所以在上面的例子中,如果我点击“This is the first sentence”中的“This”,那么所有出现的“This”都会被突出显示。

有人知道如何只突出显示双击的单词吗?

【问题讨论】:

    标签: jquery jquery-plugins


    【解决方案1】:

    如果您使用的是 jQuery,这将起作用。

    $(document).dblclick(function(){
        $("html").attr("contenteditable", "true")
        document.execCommand("hiliteColor", false, "yellow")
        $("html").attr("contenteditable", "false")
    })
    

    更新:我发了jsFiddle

    【讨论】:

      【解决方案2】:

      我最终将每个单词包装起来并为其附加了一个点击函数(jquery 调用的是 .click(function(){}) 吗?)

      我创建了两个 css 类:一个具有 background=transparent,另一个具有 background=yellow

      函数检测到类并根据需要切换它

      【讨论】:

        猜你喜欢
        • 2014-03-16
        • 2014-08-07
        • 2014-09-14
        • 1970-01-01
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        相关资源
        最近更新 更多