jQuery添加CSS样式 [英] jQuery add css style
本文介绍了jQuery添加CSS样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个文本字符串,我想用新的文本字符串替换它,但是每次我这样做时,它的CSS都消失了.然后,我制作了一个脚本,将css添加回去,但是它似乎不起作用.有什么帮助吗?
I have this text string and I would like to replace it with new text string but everytime I do it the css of it was gone. And then I make a script to add the css back but it doesn't seem to work. Any help please?
jQuery('div:contains("text")').replaceWith('newText').css({font-weight:"bolder"});
谢谢
推荐答案
使用.text()
代替:
jQuery('div:contains(text)').text('newText').css({"font-weight":"bolder"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>text</div>
.replaceWith()
方法用代码中的textNode
替换了元素,并且font-weight
css属性应该必须用引号引起来,或者使用camelCase命名约定fontWeight
.
.replaceWith()
method replaces the element with the textNode
in your code and font-weight
css property should have to be wrapped in either quotes or use camelCase naming convention fontWeight
.
这篇关于jQuery添加CSS样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文