我有一个从php文件加载的列表我试图在此列表中获取文本。当我单击此列表时,控制台会给我一个referenceError。我将仅显示带有注释的HTML代码,其中数据是从php添加的。
<div data-demo-html="true">
<ul data-role="listview" data-theme="a" data-split-theme="b" data-split-icon="plus" data-inset="true" id="courselist">
<!-- Data below loaded from PHP-->
<li id="clist">
<a href="#page">
<h2> text loaded here </h2>
</a>
</li>
</ul>
<!-- Data loading finished from PHP -->
</div>
<script>
$(function(){
var desc;
$('body').on('click','clist',function(){
desc = $(this).text();
console.log(desc);
});
});
</script>
答案 0 :(得分:1)
您的选择器错误,clist
为id
元素的li
,因此请使用'#clist'
代替'clist'
:
<script>
$(function(){
var desc;
$('body').on('click','#clist',function(){
desc = $(this).text();
console.log(desc);
});
});
</script>
有关详细信息,请参阅 jQuery Selector API
答案 1 :(得分:0)
你错过了id#selector
HTML <li id="clist">
$('body').on('click','#clist',function(){
^^^^^
desc = $(this).text();
console.log(desc);
});
阅读Jquery Selector