2013-03-22 36 views
-1

未定义的HTML如下:数据属性归国从JQuery的

<ul class="categoryitems"> 
    <li><a data-url="../po/browse_po.php" href="welcome.php" target="main">PURCHASE ORDERS</a></li>   
    <li><a data-url="../po/po_email_config.php" href="welcome.php" target="main">PO EMAILS</a></li>  
</ul> 

忽略它们是不相关的HREF中。我计划防止他们违约。

什么是培训相关的是jQuery的失败:

$('a').click(function() 
    { 
     alert($(this).parent().html()); //shows the a tag with the data-url attribute 
     //$(this).data('url',"..someurl.com"); 
     alert($(this).data('url')); //returns undefined; when the previous line is uncommented works 
     alert($(this).attr('data-url')); //this works   
    }); 

如果它的确与众不同点击功能是包裹在此:

$("#wndNavbar").ready(function() { 

}); 

因为它在一个框架:

<frameset cols="<?=$gl_navbar_width?>,*" frameborder="0" framespacing="5" border="0"> 
    <frame id="wndNavbar" name="wndNavbar" src="nav_bar.php" scrolling="auto" marginheight="0" noresize> 
    <frame name="main" src="welcome.php" scrolling="auto" marginheight="0" noresize> 
</frameset> 
+0

无法重现http://jsfiddle.net/BhmcH/ – Musa 2013-03-22 23:19:35

+0

也不是你的HTML。 – Musa 2013-03-22 23:35:44

+0

我认为我们需要看到更多的代码。特别是如果有涉及的iframe?你的iframe的id是否实际上是“wndNavbar”? – Steve 2013-03-22 23:41:10

回答

2

这不是你如何阅读data-url。这是正确的方式:

alert($(this).data('url')); 

alert($(this).attr('data-url')); 

更新:由于上述似乎并不为您解决问题,你有没有试过用

$(document).ready(function() { 

}); 

$("#wndNavbar").live(function() { 

}); 

而不是

$("#wndNavbar").ready(function() { 

}); 

更新2:您似乎正在使用frame,而不是iframeready不上frame的工作,所以你需要使用load

$("#wndNavbar").load(function() { 

}); 
+0

但是(据我所知),除了'document'。所以试试'live'。 – Steve 2013-03-22 23:25:55

+0

你是说你的iframe'id'是'#wndNavbar'?或者是iframe中的'#wndNavbar'? – Steve 2013-03-22 23:42:09

+0

很高兴我能帮上忙。 :) – Steve 2013-03-22 23:54:03