jquery – 查找文本节点

是否有一个聪明的jQuery选择器用于选择这样的文本节点:
<div><input type="text">one <span>two</span> three</div>

我想从上面的标记获取三个并将其包装在如下的强标记中:

<div><input type="text">one <span>two</span> <strong>three</strong></div>

解决方法

以下是如何使用jQuery选择文本节点:
var x = $('div') 
  .contents() 
  .filter(function() { 
    return this.nodeType == 3;
    //return this.nodeType == Node.TEXT_NODE;  this works unless using IE 7
  });

在你的例子中,x将在索引0处包含’one’,在索引1处包含’three’.就像Keith Rousseau所说,你不能真正抓住那个文本,但是如果你知道它将是最后一个你可以得到它像这样:

var elemThree = x[x.length-1];

您还可以添加如下强标记

$(x[x.length-1]).wrap("<strong></strong>");

This问题描述了使用jQuery选择文本节点(我的第一个代码片段).

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...