insertAfter不使用HTML [英] insertAfter not working with html

查看:69
本文介绍了insertAfter不使用HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我有一个 p 元素,并尝试将它封装在 span 中。 然后将
  2. 插入到标记 < p / li>

我这样试过,但是 insertAfter 并没有完成它的工作。

$ mytext = $(p#test ; $ myspan =< span style ='color:red'>+ $ mytext +< / span; $($ myspan).insertAfter(p#output);

< script src =https://ajax.googleapis.com/ajax/libs /jquery/2.1.1/jquery.min.js\"> ;</script><p id =test>测试< / p>< p id =output> OUTPUT:< p>

$ b

我也用追加这个工作:



  $ mytext = $(p#test); $ mytext_html = $ mytext.html(); $ myspan =< span style ='颜色:红'> 中+ $ mytext_html + < /跨度 ; $( p#输出)附加($ myspan); $ mytext.remove();  

< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1 .1 / jquery.min.js>< / script>< p id =test> Test< / p>< p id =output> OUTPUT:< / p> code>



如何解决这个问题? div class =h2_lin>解决方案

您可以使用 .wrap( ) 在匹配元素集中的元素周围包装HTML结构。

  $(' p'test')。wrap('< span style =color:red>< / span>'); 

 <$ ('#test#')。wrap('< span id =testspanstyle =color:red>< / span>'); $('#testspan')。insertAfter ('#output');  

< script src = https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js\"></script>< p id =test>测试< / p><< ; p id =output> OUTPUT:< / p>

  1. I have a p Element and try to wrap it inside a span.
  2. then insert it after the p tag with the id output.

I tried it like this, but the insertAfter is not doing it's job.

$mytext = $("p#test").html();
$myspan = "<span style='color:red'>"+$mytext+"</span";

$($myspan).insertAfter("p#output");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="test">Test</p>
<p id="output">OUTPUT:</p>

I also tried it with append this works:

$mytext = $("p#test");
$mytext_html = $mytext.html();

$myspan = "<span style='color:red'>"+$mytext_html+"</span";
$("p#output").append($myspan);
$mytext.remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p id="test">Test</p>
<p id="output">OUTPUT:</p>

How can i solve this?

解决方案

You can rather use .wrap() to wrap an HTML structure around element in the set of matched elements.

$('p#test').wrap('<span style="color:red"></span>');

$('p#test').wrap('<span id="testspan" style="color:red"></span>');
$('#testspan').insertAfter('#output');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<p id="test">Test</p>
<p id="output">OUTPUT:</p>

这篇关于insertAfter不使用HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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