简体   繁体   English

如何在不影响其样式的情况下将事件处理程序添加到 DOM 树中的文本节点?

[英]How can I add an event handler to a text node in DOM tree without affecting its style?

I am trying to add event handlers to words in text nodes in DOM tree without modifying how they look .我正在尝试将事件处理程序添加到 DOM 树中文本节点中的单词而不修改它们的外观 Currently I just split the words in a text node and wrap them in separate <span> tags.目前我只是在文本节点中拆分单词并将它们包装在单独的<span>标记中。

It basically turn:它基本上变成:

    <div className='parent-of-text'>
      Have a good day
    </div>

into:进入:

    <div className='parent-of-text'>
      <span onClick={doSomething}>
        Have
      </span> 
      <span onClick={doSomething}>
        a
      </span> 
      <span onClick={doSomething}>
        good
      </span> 
      <span onClick={doSomething}>
        day
      </span>
    </div>

However, in some cases, there may be some CSS rules that dictate the style of <span> that are direct descendants of parent-of-text class:但是,在某些情况下,可能有一些 CSS 规则规定了<span>的样式,它们是parent-of-text class 的直接后代:

Say:说:

.parent-of-text > span {
  color: red;
}

In cases like this, what can I do to avoid the effects of such CSS rules?在这种情况下,我该怎么做才能避免此类 CSS 规则的影响?

When you generate your span with JS (you didn't show us relevant code) apply inline style on those spans, this will overwrite presented CSS style for that element.当您使用 JS 生成跨度时(您没有向我们展示相关代码)在这些跨度上应用内联样式,这将覆盖该元素呈现的 CSS 样式。

Also this has nothing to do with DOM, JS and event handlers, this is only CSS applying styles issue.这也与 DOM、JS 和事件处理程序无关,这只是 CSS 应用 styles 问题。

 .parent-of-text>span { color: red; }
 <div class='parent-of-text'> Have a good day </div> <div class='parent-of-text'> <span onClick={doSomething} style="color: black"> Have </span> <span onClick={doSomething}> a </span> <span onClick={doSomething} style="color: black"> good </span> <span onClick={doSomething}> day </span> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何只专注于“ input:text”而不触发其绑定的焦点事件处理程序 - How can I only focus in on an “input:text” without triggering its bound focus event handler 如何在不影响其子元素的情况下更改元素的文本? - How can I change the text of an element without affecting its child element? 如何在YUI事件处理程序中获取DOM节点? - How do I get the DOM node within a YUI event handler? 如何在不影响元素的 rest 和任何事件侦听器的情况下覆盖元素文本的一部分 - How can I overwrite a portion of an element's text without affecting the rest of the element and any event listeners 如何将 DOM 事件作为事件处理程序绑定方法传递 - How can I pass the DOM event as an event handler bind method 如何在DOM树周围移动元素而不影响相关的javascript? - How to move an element around DOM tree without affecting related javascript? 如何装饰DO​​M节点的事件处理程序? - How to decorate a DOM node's event handler? 如何在事件处理程序中包含一个函数(及其参数),而无需在页面加载时调用该函数? - How can I include a function (and its parameter) within an event handler, without the function being called on page load? 如何将处理程序事件添加到数组中 - How can I add handler event into an array 如何使用复制/克隆在实时DOM中找到文本节点? - How can I find a text node in the live DOM using its copy/clone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM