Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery选择器中的帮助_Javascript_Jquery_Html_Css_Jquery Selectors - Fatal编程技术网

Javascript jquery选择器中的帮助

Javascript jquery选择器中的帮助,javascript,jquery,html,css,jquery-selectors,Javascript,Jquery,Html,Css,Jquery Selectors,您好,请参阅以下HTML代码: <div id="content"> <p> <font size='2'> <img src="something.jpg" width="500" height="500" /> this is the text content. this is the text content. this is the text content... </f

您好,请参阅以下HTML代码:

<div id="content">
    <p>
      <font size='2'>
        <img src="something.jpg" width="500" height="500" />
        this is the text content. this is the text content. this is the text content...
      </font>
    </p>
</div>


这是文本内容。这是文本内容。这是文本内容。。。

这是从我的web应用程序的管理部分生成的html代码。我不能修改它,也不能改变上面代码的结构。它是动态生成的

我需要使用jquery wrap()方法将文本内容包装到html元素中,这样我就可以在文本内容而不是img元素上放置一些css

注意:id为“content”的div元素是我自己的,我只能使用HTML访问它。

请帮忙怎么做


谢谢

我不确定用jQuery是否可以做到这一点。但是如果您使用原始JavaScript,您可以查看节点并查看它们的
节点类型
,其中1为元素,3为文本。所以说
f
是您的字体元素:

for (var i=0; i < f.childNodes.length; i++) {
    if (f.childNodes[i].nodeType == 3) {
        var text = f.nodeValue;
        // Remove the text node, insert a new span node and plug in the text.
    }
}
for(var i=0;i
以下是jQuery方法

$("p > font")
  .contents()
  .filter(function() {
    return this.nodeType == 3;
  }).wrap('<span style="color:#FF0000" />');
$(“p>font”)
.contents()
.filter(函数(){
返回this.nodeType==3;
}).wrap(“”);

你可以试试这个

$(function() {  
    $("#content").find("p > font").each(function() {
    var $this = $(this).wrapInner(document.createElement("div"));
    var $img = $this.find('img').remove();
    $this.prepend($img);
    })
}); 
<p>
   <img height="500" width="500" src="something.jpg">
    <div>
        this is the text content. this is the text content. this is the text content...
    </div>
</p>
它将返回这个

$(function() {  
    $("#content").find("p > font").each(function() {
    var $this = $(this).wrapInner(document.createElement("div"));
    var $img = $this.find('img').remove();
    $this.prepend($img);
    })
}); 
<p>
   <img height="500" width="500" src="something.jpg">
    <div>
        this is the text content. this is the text content. this is the text content...
    </div>
</p>

这是文本内容。这是文本内容。这是文本内容。。。


我尝试使用这个变量elm=$(“#content p font”).children(“:not(img)”).wrap(”;谢谢,但是我只想包装“文本内容”,而不是img元素。好的,我修改了上面的代码。John的示例将产生一些额外的空标记,但仍然有效。-祝你好运