如何在页面上搜索所有翻译属性并替换其值?

时间:2017-11-07 18:25:45

标签: javascript jquery angular dom translate

如何查询整个HTML页面以查找特定属性并将其删除或替换值?

我在想这样的事情(翻译是属性名称):

$('[translate]').remove();

$('[translate]').value('replace the value of the attribute');

2 个答案:

答案 0 :(得分:0)



$(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>
&#13;
&#13;
&#13;

嗨!这段代码可能对您有所帮助。

答案 1 :(得分:0)

以下是一个示例: - 您可以使用nativeElement&amp;用于DOM操作的querySelector`。

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', '');
    });