javascript - js如何获取点击的li中a的text()?
PHPz
PHPz 2017-04-10 17:41:27
0
8
359
<ul class="second-tag">
    <li><a class="tag-name">我是1</a><span>400</span></li>
    <li><a class="tag-name">我是2</a><span>340</span></li>
    <li><a class="tag-name">啦啦啦</a><span>313</span></li>
    <li><a class="tag-name">嘿嘿</a><span>298</span></li>
    <li><a class="tag-name">哇哦</a><span>130</span></li>
</ul>
$("document").ready(function(){
    $(".second-tag li").click(function(){
        $val = $(".tag-name").text();
        $url = window.location.href;
        window.location.href = 'https://xxx/tag/' + $val;
    })
})

如上代码,当点击li时获取到了所有的.tag-name中的文本,如何做到点击哪个li,只获取点击的li中的.tag-name的文本?

PHPz
PHPz

学习是最好的投资!

répondre à tous(8)
Peter_Zhu
$("document").ready(function(){
    $(".second-tag li").click(function(){
        var liTextStr = $(this).text();
        $url = window.location.href;
        window.location.href = 'https://xxx/tag/' + liTextStr;
    })
})
迷茫

$(this)

刘奇
$("document").ready(function(){
    $(".second-tag li").click(function(){
        $val = $(this).find(".tag-name").text();
        window.location.href = 'https://xxx/tag/' + $val;
    })
})
PHPzhong
$("document").ready(function(){
    $(".second-tag li").click(function(){
        $val = $(this).text();
        $url = window.location.href;
        window.location.href = 'https://xxx/tag/' + $val;
    })
})
左手右手慢动作
$('.second-tag').on('click', 'a', function(){
  $val = $(this).text();
  $url = window.location.href;
  window.location.href = 'https://xxx/tag/' + $val;
  return false;
})
Ty80

$("document").ready(function(){

$(".second-tag li").click(function(){
    window.location.href = 'https://xxx/tag/' + $(this).text();
})

})

左手右手慢动作

$(".second-tag li").click(function(e){

    $val = $(e.currentTarget).text();
    $url = window.location.href;
    window.location.href = 'https://xxx/tag/' + $val;
})

联系再深入了解一下事件对象

PHPzhong

极简

$(function () {
    $(".second-tag>li").click(function () {
        location.href = 'https://xxx/tag/' + $(this).children("a").text();
    });
});
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal