简体   繁体   English

jQuery .Text()函数正在替换元素中的HTML

[英]jQuery .Text() function is replacing HTML within element

I am trying to replace text within an element on the fly, however .text() function is replacing both text and HTML. 我正在尝试动态替换元素内的文本,但是.text()函数正在替换文本和HTML。 Any thoughts on how I only edit the actual text? 关于如何仅编辑实际文本有任何想法吗?

<div>
  <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a>
<div>

I want to change the words "My Title" to "My New Title", however whenever I use .text() function, it replaces all of my html and my anchor tag disappears. 我想将单词“我的标题”更改为“我的新标题”,但是,每当我使用.text()函数时,它都会替换我所有的html,而我的定位标记也消失了。

Here is my jQuery: 这是我的jQuery:

$('h3.titleHeading').text("My New Title")

My output is then - again, the code removes my anchor tag which I need for a tooltip. 然后,我的输出是-再次,代码删除了我需要工具提示的锚标记。

<div>
  <h3 class="titleHeading">My New Title 
<div>

When developing HTML, if you have an element with child HTML elements, it is a bad idea to have it contain plain-text as well. 开发HTML时,如果您的元素包含子HTML元素,则也要使其包含纯文本是个坏主意。 This causes problems like what you see here. 这会引起类似您在此处看到的问题。 An easy solution is to put your text inside a span . 一个简单的解决方案是将您的文本放在span (Note: Don't forget to close your h3 .) (注意:不要忘记关闭h3 。)

<div>
  <h3 class="titleHeading">
    <span id="titleHeadingText">My Title</span>
    <a ng-click="openTooltip('getMoreInfo')">
      <span class="toolTip"></span>
    </a>
  </h3>
<div>

You can change the elements of the text easily, now, without affecting the other elements. 现在,您可以轻松更改文本的元素,而不会影响其他元素。

$("#titleHeadingText").text("My New Title");

You should do a few things as outlined in the code below 您应该执行以下代码中概述的一些操作

  1. Close the <h3> element 关闭<h3>元素
  2. Wrap the text you want to change in a span 自动换行要在改变span

Example

<div>
  <h3 class="titleHeading">
    <span class="titleHeadingText"> My Title</span>
    <a ng-click="openTooltip('getMoreInfo')">
      <span class="toolTip"></span>
    </a>
  </h3> 

jQuery jQuery的

$('h3.titleHeadingText').text("My New Title")

To achieve expected result, just update innerText for h3 by contents() data instead of using text() 为了获得预期的结果,只需通过contents()数据更新h3的innerText,而不要使用text()

  1. Jquery contents() will return object of DOM elements jQuery contents()将返回DOM元素的对象
  2. 'data' property will provide the innerText of element “数据”属性将提供元素的innerText

 $('.titleHeading').contents()[0].data = "My New Title" 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a> <div> 

codepen - https://codepen.io/nagasai/pen/YoXXjd?editors=1010 codepen- https: //codepen.io/nagasai/pen/YoXXjd ? editors = 1010

If you do not have access to the code, loop through only the text nodes and replace their textContent with your new content. 如果您无权访问代码,则仅遍历文本节点并将其textContent替换为新内容。 This will keep the other existing HTML intact. 这将使其他现有的HTML保持不变。

Taken from this answer . 取自这个答案

 $('h3.titleHeading').contents().filter(function() { return this.nodeType == 3 }).each(function(){ this.textContent = this.textContent.replace('My Title','My New Title'); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a> <div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM