开发者

href is always "undefined"

On alert I always get undefined... why ?

<script type="text/javascript">

$(document).ready(function(){
    $("#menu ul li").click(function() {
        var path = $(this).attr("href"); 
        alert (path);
        $.get(path, function(data) { $("#texte").html(data); });
        return false;
    });
});

</script>

</head>

<body>

<div id="wrapper">

<div id="menu">
  <ul>
    <li><a href="pulse/data/blocks/intro.html">Intro</a></li>
    <li><a href="pulse/data/blocks/presentation.html">presentation</a></li>
    <li><a href="pulse/data/blocks/pourquoi.html">pourquoi ?</a></li>
    <li><a href="pulse/d开发者_高级运维ata/blocks/forfaits.html">forfaits</a></li>
  </ul>
</div>


You need to select the a tag.

var path = $('a', this).attr("href"); 


You need to select element:

$(document).ready(function(){
    $("#menu ul li").click(function() {
        var path = $(this).find("a").attr("href"); 
        alert (path);
        $.get(path, function(data) { $("#texte").html(data); });
        return false;
    });
});


Because you're assigning the click-handler on the LI elements, not the A elements.

This is correct:

$("#menu a")


You are looking at the href of your list item (li). Change your code to the following:

$("#menu ul li a").click(function() { 


You are trying to get the href attribute of the li element. You are probably looking for the href attribute on the a element instead. Use this selector instead:

$("#menu ul li a")

It can probably be reduced by removing either the ul or li from the selector as well.


The trouble is that $(this) references the <li> element instead of it's link. You can fix this by using the following selector:

var path = $("a", $(this)).attr("href"); 


you have missed a tag.

$("#menu ul li a").click


Your JQuery is referencing the wrong element: Instead of referencing the UL Li element you need to reference the UL LI A element as shown below:

$(document).ready(function(){
    $("#menu ul li **a**").click(function() {
        var path = $(this).attr("href"); 
        alert (path);
        $.get(path, function(data) { $("#texte").html(data); });
        return false;
    });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜