简体   繁体   English

.text()影响h1内的html元素

[英].text() affects html elements inside h1

I have the following markup: 我有以下标记:

<h1>A text <span class="createTask glyphicon glyphicon-plus"></span></h1>

What I want to do is when I double click an h1 to change its text. 我要做的是双击h1更改其文本。 To do this I wrote the following code inside a document ready function 为此,我在文档就绪函数中编写了以下代码

$("h1").on("dblclick",function(){
   var newTitle = prompt("Enter a new title");
  if(newTitle){
     $(this).text(newTitle);
  }

})

However this code instead of just changing the text of the h1 it removes the glyphicon span. 但是,此代码不只是更改h1的文本,而是删除了glyphicon跨度。 Any Ideas why? 任何想法为什么? Also note that I cannot change the markup . 请注意,我无法更改标记

Using $(this).text() ( .html() is the correct syntax), you are changing the entire h1 content, including the glyph inside it. 使用$(this).text() (. .html()是正确的语法),您正在更改整个h1内容,包括其中的字形。

If you want to keep the glyph (with the span), seperate them from the h1 like so: 如果要保留字形(带有跨度),则将它们与h1分开,如下所示:

<h1>A text </h1><span class="createTask glyphicon glyphicon-plus"></span>

Or, as you requested - without changing the markup: 或者,根据您的要求-无需更改标记:

$(this).html(newTitle + "<span class='createTask glyphicon glyphicon-plus'>");

You need to update the first text node within the h1 : $(this).contents().get(0).nodeValue = newTitle; 您需要更新h1的第一个文本节点: $(this).contents().get(0).nodeValue = newTitle; .

 $("h1").on("dblclick", function() { var newTitle = prompt("Enter a new title"); if (newTitle) { $(this).contents().get(0).nodeValue = newTitle; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <h1>A text <span class="createTask glyphicon glyphicon-plus"></span></h1> 

Add another span inside the h1 element and then h1元素内添加另一个范围,然后

<h1>
    <span id="text">A text </span>
    <span class="createTask glyphicon glyphicon-plus"></span>
</h1>

$("h1").on("dblclick",function() {
    var newTitle = prompt("Enter a new title");
    if (newTitle){
        $(this).find('#text').text(newTitle);
    }

})

You can use Node.previousSibling to get previous sibling of SPAN DOM element and then its nodeValue can be set. 您可以使用Node.previousSibling获取SPAN DOM元素的先前同级,然后可以设置其nodeValue

 $("h1").on("dblclick", function() { var newTitle = prompt("Enter a new title"); if (newTitle) { var span = $(this).find('span').get(0); //get the DOM element span.previousSibling.nodeValue = newTitle; } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <h1>A text <span class="createTask glyphicon glyphicon-plus">+++++</span></h1> 

.text() replaces the contents of the element with the specified text. .text()用指定的文本替换元素的内容。 Since the icon span is inside the element it will be replaced. 由于图标范围位于元素内部,因此它将被替换。
You can target the text node directly and replace its text. 您可以直接定位文本节点并替换其文本。

this.firstChild.nodeValue = newTitle;

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

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