事件监听器(onClick事件)永远不会被附加。
有什么想法吗?
MyPage.html有:
<p>
<a href="#" class="js-expander expander">Learn more</a></p>
<div class="js-expander-content hidden"><p>
Hidden-text-goes-here </p>
</div>
<p />
</div>
</body>
expander.js是:
/* expander control */
debugger; // THIS EXECUTES
$().ready(function() {
debugger; //THIS NEVER EXECUTES
$(document).on('click', '.js-expander', function(e){
var e = e || event,
$el = $(this),
$content;
e.preventDefault();
$content = $el.parent().next('.js-expander-content')
if ($content.length == 0)
$content = $el.parent().parent().next('.js-expander-content')
if ($content.length == 0)
$content = $el.siblings('.js-expander-content');
$content.slideToggle(150);
})
})
答案 0 :(得分:0)
只要正确引用了JQuery,您的代码就会执行。
仅供参考 - 你确实有几个HTML语法错误,你有一些过时的和不必要的JavaScript(即e = e || event
),除非你仍然需要支持IE8。
$(function() {
$(document).on('click', '.js-expander', function(e){
$el = $(this),
e.preventDefault();
$content = $el.parent().next('.js-expander-content')
if ($content.length == 0){
$content = $el.parent().parent().next('.js-expander-content');
}
if ($content.length == 0){
$content = $el.siblings('.js-expander-content');
}
$content.slideToggle(150);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<a href="#" class="js-expander expander">Learn more</a>
</p>
<div class="js-expander-content hidden">
<p>Hidden-text-goes-here</p>
</div>
&#13;