2 回答
TA贡献1799条经验 获得超9个赞
// Find correct element
const el = document.querySelector(".js-property-select.selected");
// Create new tag-indicator
const newElement = document.createElement('div');
newElement.className = "tag-indicator";
// Append to active element
el.appendChild(newElement);
// Optional; remove it
const toRemove = el.querySelector(".tag-indicator");
el.removeChild(toRemove);
.tag-indicator {
height: 10px;
border: 1px dotted orange;
}
<ul id="property-select-list">
<li class="js-property-select" data-id="1" data-has_media_alt="YES">
prperty1
</li>
<li class="js-property-select selected" data-id="2" data-has_media_alt="NO">
prperty2
<div class="tag-indicator"></div>
</li>
<li class="js-property-select" data-id="3" data-has_media_alt="NO">
prperty3
<div class="tag-indicator"></div>
</li>
</ul>
jQuery
// Add
$('.js-property-select.selected').append('<div class="tag-indicator"></div>');
// Optional; Remove
$('.js-property-select.selected > .tag-indicator').remove();
.tag-indicator {
height: 10px;
border: 1px dotted orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="property-select-list">
<li class="js-property-select" data-id="1" data-has_media_alt="YES">
prperty1
</li>
<li class="js-property-select selected" data-id="2" data-has_media_alt="NO">
prperty2
<div class="tag-indicator"></div>
</li>
<li class="js-property-select" data-id="3" data-has_media_alt="NO">
prperty3
<div class="tag-indicator"></div>
</li>
</ul>
TA贡献1780条经验 获得超3个赞
添加特定的 HTML
// Add the html only if it does not exists already
if (!$('.js-property-select.selected > .tag-indicator').length) {
$('.js-property-select.selected').append('<div class="tag-indicator"></div>');
}
删除特定的 HTML
$('.js-property-select.selected > .tag-indicator').remove();
- 2 回答
- 0 关注
- 79 浏览
添加回答
举报