$("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的文本?
极简
$(function () { $(".second-tag>li").click(function () { location.href = 'https://xxx/tag/' + $(this).children("a").text(); }); });
$(".second-tag li").click(function(e){
$val = $(e.currentTarget).text(); $url = window.location.href; window.location.href = 'https://xxx/tag/' + $val; })
联系再深入了解一下事件对象
$("document").ready(function(){
$(".second-tag li").click(function(){ window.location.href = 'https://xxx/tag/' + $(this).text(); })
})
$('.second-tag').on('click', 'a', function(){
$val = $(this).text();
$url = window.location.href;
window.location.href = 'https://xxx/tag/' + $val;
return false;
})
$("document").ready(function(){ $(".second-tag li").click(function(){ $val = $(this).text(); $url = window.location.href; window.location.href = 'https://xxx/tag/' + $val; }) })
$("document").ready(function(){ $(".second-tag li").click(function(){ $val = $(this).find(".tag-name").text(); window.location.href = 'https://xxx/tag/' + $val; }) })
$(this)
$("document").ready(function(){ $(".second-tag li").click(function(){ var liTextStr = $(this).text(); $url = window.location.href; window.location.href = 'https://xxx/tag/' + liTextStr; }) })