如何查询整个 HTML 页面中的特定属性并删除它或替换值?
我在想这样的事情(翻译为属性名称):
$('[translate]').remove();
$('[translate]').value('replace the value of the attribute');
这里有一个例子:- 你可以使用 nativeElement & querySelector' 进行 DOM 操作。
app.component.html
<div class="tabs tabsMenu" #tabs>
abc
</div>
<div id="tab-1" class="tab-content rmpm" role="tabpanel">abc</div>
<div id="tab-2" class="tab-content rmpm" role="tabpanel">abc</div>
app.component.ts
@ViewChild('tabs') public tabs;
constructor(private el: ElementRef) {
this.el = el;
}
let tabsEl = this.tabs.nativeElement;
this.removeActive(tabsEl.querySelectorAll('.active'));
this.setActive([
tabsEl.querySelector(`#${id}`)
]);
setActive(elems) {
elems.forEach((el) => {
el.className += ' active';
});
}
removeActive(elems) {
elems.forEach((el) => {
el.className = el.className.replace(' active', '');
});
$(document).ready(function() {
$('a').click(function(v){
var changeVal = "other_val"
$("[translate]").attr("translate",changeVal)
$("[translate]").html(changeVal)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div translate="some_val">some_val
</div>
<div translate="some_val">some_val
</div>
<a href="javascript:void(0)">Change attr</a>
你好!此代码段可能对您有所帮助。