如何更改<ul>中的<li>值

时间:2019-10-06 08:03:39

标签: javascript jquery html tags

我想更改没有任何id的li的特定值,如何访问此li并将其更改为另一个值。 在以下代码中,我想使用jquery或javascript将“编辑为放置”更改为“编辑”。

<ul class="tabs--primary nav nav-tabs">
    <li class="active"><a href="/packages/50411" data-drupal-link-system-path="packages/50411" class="is-active">View<span class="visually-hidden">(active tab)</span></a></li>
    <li><a href="/packages/50411/edit" data-drupal-link-system-path="packages/50411/edit">Edit</a></li>
    <li><a href="/packages/50411/delete" data-drupal-link-system-path="packages/50411/delete">Delete</a></li>
    <li><a href="/packages/50411/edit/enrollment_drop_mode" data-drupal-link-system-path="packages/50411/edit/enrollment_drop_mode">Edit as Drop</a></li>
    <li><a href="/devel/packages/50411" data-drupal-link-system-path="devel/packages/50411">Devel</a></li>
</ul>

3 个答案:

答案 0 :(得分:1)

您可以尝试使用jQuery的:contains()选择器,该选择器选择所有包含指定文本的元素

$('ul li a:contains(Edit as Drop)').text('Edit');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="tabs--primary nav nav-tabs">
    <li class="active"><a href="/packages/50411" data-drupal-link-system-path="packages/50411" class="is-active">View<span class="visually-hidden">(active tab)</span></a></li>
    <li><a href="/packages/50411/edit" data-drupal-link-system-path="packages/50411/edit">Edit</a></li>
    <li><a href="/packages/50411/delete" data-drupal-link-system-path="packages/50411/delete">Delete</a></li>
    <li><a href="/packages/50411/edit/enrollment_drop_mode" data-drupal-link-system-path="packages/50411/edit/enrollment_drop_mode">Edit as Drop</a></li>
    <li><a href="/devel/packages/50411" data-drupal-link-system-path="devel/packages/50411">Devel</a></li>
</ul>

答案 1 :(得分:0)

您可以使用nth-child()解决此问题,例如:

$( ".nav-tabs li:nth-child(4) a" ).html( "Edit" );

答案 2 :(得分:0)

我可以感觉到反对意见的到来...但是为什么不添加ID并使用innerHTML将值设置为另一个String testString = String( (char*) packet.data());