首页 > 解决方案 > 当标签中没有 id 名称或特定类时,如何使用 javascript 向 HTML 添加文本?

问题描述

我想使用 javascript 在锚标记中向我的 HTML 页面添加一些文本,但该锚标记中没有 id、name 或 class。有没有办法做到这一点。示例代码如下。

<div>
  <table>
    <tbody>
      <tr>
        <th>
          <span ng-if="smData.standard==smData.stdnr" class="ng-scope"> 
          <a ng-click="m1(a,b)" class="ng-binding" href="/">
              Hello.
          </a> 
        </span>
        </th>
      </tr>
    </tbody>
  </table>
</div>

现在我想在 Hello(say world.) 中添加一些内容。

标签: javascripthtmldom

解决方案


使用属性选择器:

document.querySelector("a[ng-click='m1(a, b)']").innerText += " world.";
<a ng-click="m1(a, b)" class="ng-binding" href="/">Hello</a>


推荐阅读