开发者

Change variable every time a link clicked

links:

<ul id="top开发者_运维知识库ics">
<? while ($row = mysql_fetch_object($result)) { ?>
<li>- <a href="#" title="<?=$row->t_topic;?>"><?=$row->t_topic;?></a></li>
<? } mysql_free_result($result); ?>
</ul>

jQuery code:

$(function() {
    $("a").click(function(){
        var title = $("a").attr("title");
        $("#main").html(title);
    });
});

'title' is different on every link. When I clicked a link, it doesn't read var 'title'.


The code needs to read the title from a specific a tag that was clicked upon. This simple change should do it:

$(function() {
    $("a").click(function(){
        var title = $(this).attr("title"); // Note "this" here
        $("#main").html(title);
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜