简体   繁体   English

如何通过jQuery使用div ID删除div中的特定html标签及其内容?

[英]How to remove a particular html tag and its content which is inside a div, using div id by jquery?

I have the following div tag, which will be created onload by jquery. 我有以下div标签,它将由jquery在加载时创建。 I want to remove only the span tag and its contents which is being created inside the div. 我只想删除在div中创建的span标签及其内容。 How can i do this using jquery ? 我如何使用jquery做到这一点? I have tried innerHTML but it deletes all the contents of the div with id uniform-qualification, as I want to delete only the span tag below. 我已经尝试了innerHTML,但是由于我只想删除下面的span标签,所以它会删除id统一限定符的div的所有内容。

<div id="uniform-qualification" class="">
<span>BCh</span>
 </div>

Note : the span tag cannot be given with an id as its being created dynamically from the UI plugin 注意:span标签不能带有id,因为它是通过UI插件动态创建的

$("#uniform-qualification > span").remove();

but you'll need to provide more information if you want a more informed answer. 但是如果您想获得更明智的答案,则需要提供更多信息。 For example, if you have more than one span but only want to remove the first one you'll need something like: 例如,如果您有多个跨度,但只想删除第一个跨度,则需要类似以下内容:

$("#uniform-qualification > span:eq(0)").remove();

To remove all span elements within the div try the following: 要删除div中的所有span元素,请尝试以下操作:

$('#uniform-qualification span').remove();

To remove only child span elements: 要仅删除子范围元素:

$('#uniform-qualification > span').remove();

To remove only the first child span element: 要仅删除第一个子span元素,请执行以下操作:

$('#uniform-qualification > span').first().remove();
$('#uniform-qualification').html('');

这样就可以了。

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

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