第一个文本内容的jQuery选择器
jQuery selector for first text content
是的,我同意我应该从文档中找出这一点,但是我不能:-(
)<div id="jalla">
<span>
<span class="ui-btn-text">
I need this text
<span class="ui-collapsible-heading-status">
click to collapse contents
</span>
</span>
<span>
</div>
如何简单地选择(和更新)文本"我需要这个文本"。这是我尝试过的方法之一:
notWorking = $('.ui-btn-text').text();
有时候jQuery并不是最好的答案。在这种情况下,HTML DOM使它更容易。
var span = document.querySelector('.ui-btn-text'); // first item with this class
var text = span.childNodes[0]; // an actual text node
text.nodeValue = "Changed!" // done!
实际应用:http://jsfiddle.net/C7Q6K/
编辑:对于较旧的浏览器,您可以使用jQuery来获取span
本身:
var span = $('.ui-btn-text')[0]; // get the DOM element, not the jQuery object
span.childNodes[0].nodeValue = "Changed!";
相关文章:
- jQuery选择器无法正常工作
- 将jquery选择器转换为数组
- JavaScriptDOM正常工作时JQuery选择器不工作
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 换行符插入jquery选择器
- 使用javascript"这个“;用于jquery选择器
- jQuery选择器缓存问题
- jQuery 选择器,所有跨度都在 DIV 内
- JQuery选择器:如果同级具有.class,则选择td
- jQuery选择器不识别任何动态创建的HTML输入函数
- 循环遍历元素jquery选择器
- jQuery选择器错误:无法识别的表达式
- 自定义jquery选择器属性未在模拟器中激发
- Jquery选择器为空,我应该使用哪个作用域
- jQuery选择器就是用这个构建的
- jQuery 选择器混淆
- 将jQuery选择器与“this”相结合
- jQuery选择器在脚本中不起作用,但在控制台中工作
- jQuery 选择器中的简单 jQuery 语法和串联
- 全局 JQuery 选择器缓存以提高性能