使用Javascript删除除文本之外的所有内部HTML

时间:2017-08-09 02:20:03

标签: javascript html twitter-bootstrap image

我有以下HTML代码 -

<label id="label_id" >
   What is your name? 
   <img src=/info_icon.gif" id="tooltip">
   <div id="tooltip_modal">
      <div class="modal-content">
         <div class="modal-header">
            <span id="tooltip_close">&times;</span>
            <h5>Information</h5>
         </div>
         <div class="modal-body">
            <p> Some Sample text
            </p>
         </div>
      </div>
   </div>
</label>

使用JavaScript我提取了<label>这样的元素 -

var myLabel = document.getElementById("label_id");

除了<label>

之外,我想摆脱What is your name?元素中的所有内容

请注意“你叫什么名字?”表示将动态生成并插入<label>元素的一段文本。

我如何实现这一目标?

提前致谢!

3 个答案:

答案 0 :(得分:1)

获取第一个文本节点,这将是“你的名字是什么?”&#39;并用它替换var myLabel = document.getElementById("label_id"); // get the first text node var textNode = myLabel.childNodes[0]; // replace the content of label by the text myLabel.textContent = textNode.textContent;的内容 - 见下面的演示:

&#13;
&#13;
<label id="label_id" >
   What is your name? 
   <img src="/info_icon.gif" id="tooltip"/>
   <div id="tooltip_modal">
      <div class="modal-content">
         <div class="modal-header">
            <span id="tooltip_close">&times;</span>
            <h5>Information</h5>
         </div>
         <div class="modal-body">
            <p> Some Sample text
            </p>
         </div>
      </div>
   </div>
</label>
&#13;
class Shops {
    private var _familiy_id: String?
    private var _logo : String?
    private var _shopname : String?


    var familiy_id : String{
        return _familiy_id!
    }

    var shopname : String{
        return _shopname!
    }
    var Logo : String{
        return _logo!
    }

    init(shopname : String , Logo : String , family_id : String) {        
        self._shopname = shopname
               self._logo = Logo
        self._familiy_id = family_id        
    }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

  

myLabel.innerHtml =&#39;你的名字是什么?&#39;;

答案 2 :(得分:0)

var myLabel = document.getElementById("label_id");
myLabel.innerHTML = 'What is your name?';
<label id="label_id" >
   What is your name? 
   <img src="/info_icon.gif" id="tooltip"/>
   <div id="tooltip_modal">
      <div class="modal-content">
         <div class="modal-header">
            <span id="tooltip_close">&times;</span>
            <h5>Information</h5>
         </div>
         <div class="modal-body">
            <p> Some Sample text
            </p>
         </div>
      </div>
   </div>
</label>