如何更改页面上的文本? (使用jQuery查找和替换?) [英] How to change text on a page? (Find and replace using jQuery?)

查看:104
本文介绍了如何更改页面上的文本? (使用jQuery查找和替换?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个硬编码的,不可编辑的HTML块:

I have a block of hardcoded, uneditable HTML:

<td id="v65-onepage-ordercomments-value" width="61%" valign="top" colspan="2">
     Order Comments:&nbsp;(Optional)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <br>           
     <textarea name="Order_Comments" id="v65-onepage-ordercomments-input" rows="3" cols="55"></textarea>
</td>

我想替换订单注释(可选)以及所有那些不间断的空格。我最好喜欢用其他HTML替换它,如头和段落标签。什么是最好的方法做到这一点?我的假设是做一些事情像一个查找&替换使用jQuery?

I want to replace "Order Comments (Optional)", as well as all those non-breaking spaces. I would preferably like to replace it with other HTML, like a header and a paragraph tag. What is the best way to do this? My assumption is to do something like a find & replace using jQuery?

推荐答案

它总是一个textNode是父div的第一个孩子, p>

It it's always a textNode that is the first child of the parent div, you can do :

var node = document.getElementById('v65-onepage-ordercomments-value').firstChild;
node.nodeValue = 'new content';

FIDDLE

或在jQuery中:

$('#v65-onepage-ordercomments-value').contents().each(function() {
    if (this.nodeType===3 && this.nodeValue.trim().length) 
        this.nodeValue = 'new content';
});

FIDDLE

这篇关于如何更改页面上的文本? (使用jQuery查找和替换?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆