简体   繁体   English

如何使用jQuery在另一个HTML元素周围添加HTML元素

[英]How to add html element around another html element using jquery

Another day, another problem. 改天,另一个问题。 I have something like that: 我有这样的事情:

<p class="checkbox">
   <input name="cgv" id="cgv" value="1" type="checkbox">
   <label for="cgv">Some text.</label>
</p>

Using jquery I want to change this code to: 使用jQuery,我想将此代码更改为:

<p class="checkbox">
   <div class="checker" id="uniform-cgv">
      <span>
         <input name="cgv" id="cgv" value="1" type="checkbox">
      </span>
   </div>
   <label for="cgv">Some text.</label>
</p>

So, the input element #cgv is inside span and div elements. 因此,输入元素#cgv在span和div元素内部。 I want to do this by this code but without any results. 我想通过此代码执行此操作,但没有任何结果。

$('#submitGuestAccount').click(function () {
        //True if parent is <p> element
        if ($('#cgv').parent().hasClass('checkbox')) {
            $('<span>').insertBefor('#cgv');
            $('</span>').insertAfter('#cgv');

        }
    });

Is anyone can help me with my problem ? 有人可以帮助我解决我的问题吗?

Kind regards 亲切的问候

The jQuery API is well documented : http://api.jquery.com/wrap/ . jQuery API有充分的文档记录: http : //api.jquery.com/wrap/

 $("#cgv").wrap( "<div class=\\"checker\\" id=\\"uniform-cgv\\"><span></span></div>" ); 
 div{border:10px solid red} span{border:10px solid blue} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="checkbox"> <input name="cgv" id="cgv" value="1" type="checkbox"> <label for="cgv">Some text.</label> </p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM