我如何正确编写jQuery脚本 [英] How do I correctly write jquery script

查看:73
本文介绍了我如何正确编写jQuery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试更改html列表中各个链接显示的文本.

I have been attempting to change the text that appears for various links within an html list.

我想做的是这样的: 因此在小提琴中,使用jQuery-我要转到#athletic-group-下拉菜单中,在第一个ul中将第二个li内的"a"文本从新闻"更改为概述"

What I want to do is this: So in this fiddle, using jQuery - I want to go to #athletic-group-dropdown, in the first ul change the ‘a’ text that is within the second li from "News" to "Overview"

我尝试了各种脚本,这使我最接近,但:

I have tried various scripts and this gets me the closest but:

jQuery( "#athletic-group-dropdown ul li:nth-child(2)").addClass( "overview-switch" );
jQuery(".overview-switch a:contains(‘News’)").html("Overview");

jQuery("#athletic-group-dropdown ul li:nth-child(2) ").addClass("overview-switch");
jQuery(".overview-switch-lst a:contains('News')").html("Overview");

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="athletic-group-dropdown">
  <ul class="athletic-menu">
    <li>Tea</li>
    <li><a href="#">News</a></li>
    <li>Milk</li>
  </ul>

  <ul class="athletic-menu">
    <li><a href="#">News</a></li>
    <li>Coffee</li>
    <li>Peaches</li>
  </ul>

  <ul class="athletic-menu">
    <li><a href="#">News</a></li>
    <li>Apple</li>
    <li>Pear</li>
    <li>Grape</li>
  </ul>
</div>

我已经尝试了几个脚本,但都遇到了各种问题,但是我与上面的脚本很接近,但是它更改了几个链接,而不是我想要的特定链接.

I have attempted several scripts with various issues, but I am close with the above, but it changes several of the links instead of the specific one I want.

任何帮助将不胜感激.

这只是一个示例列表,在实际页面中具有6列链接,因此我希望能够通过ID选择所需的列,然后进入ul/li,找到'a'并更改该文本.

推荐答案

完美的解决方案-真正简洁,灵活:

Perfect solution here - really clean and concise and flexible:

$("#athletic-group-dropdown ul:eq(1) li:eq(2) a")
    .text("not peaches")
    .attr("href", "http://google.com");

这篇关于我如何正确编写jQuery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆