我有一个来自系统自动生成的标题。 HTML 如下所示:
$("#foo2").contents().filter(function(){
return this.nodeType == 3;
})[0].nodeValue = "" ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Auto-generated text
<div id="container"></div>
<div id="menu"></div>
<ul >
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
我能够选择文本并将其删除(请参阅我的 JS),但现在我想通过 jQuery 或 JS 设置它的样式。有谁知道如何设计它的风格吗?或者如何将其附加到 div#container
以便我可以设置它的样式。
最佳答案
使用.appendTo( target )
将文本节点移动到 #container
Description: Insert every element in the set of matched elements to the end of the target.
target
Type: Selector or htmlString or Element or Array or jQuery A selector, element, HTML string, array of elements, or jQuery object; the matched set of elements will be inserted at the end of the element(s) specified by this parameter.
$("#foo2").contents().filter(function(){
return this.nodeType == 3;
}).appendTo("#container");
#container { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo2"> Auto-generated text
<div id="container"></div>
<div id="menu"></div>
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
关于javascript - 如何设置 div 外部文本的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49073184/