此处{L}在Lire la suite上的http://dev.arphilvolis.fr/点击我需要展开div,然后在另一个点击时折叠div。
我的HTML结构:
<p style="text-align: left;">some content here</p>
<p style="text-align: left;">some content here</p>
<span class="collapslink">Lire la suite</span>
<div class="collapscontent">
<p style="text-align: left;">content</p>
<p style="text-align: left;">content</p>
</div>
我的jQuery代码:
jQuery(".collapslink").click(function() {
$collapslink = $(this);
//getting the next element
$collapscontent = $collapslink.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$collapscontent.slideToggle(500, function () {
//execute this after slideToggle is done
//change text of header based on visibility of content div
$collapslink.text(function () {
//change text based on condition
return $collapscontent.is(":visible") ? "Fermer" : "Lire la suite";
});
});
});
JSFiddle http://jsfiddle.net/94150148/vfcau364/20/正在运作但不在我的网站上:http://dev.arphilvolis.fr/
答案 0 :(得分:0)
您的实时网站中的按钮处理程序事件在 $(document).ready()
之外。由于您的处理程序不等待加载页面,因此您尝试将其附加到的<span>
尚不存在。处理程序附加到 nothing 。
要正确附加处理程序,需要在内jQuery(document).ready(function() { .... });
您网站上的代码段演示了该问题:
jQuery(document).ready(function(){
new Slideshowck(...);
});
var ampzSettings = {...};
//YOUR CODE HERE, OUTSIDE DOCUMENT READY HANDLER
jQuery(".collapslink").click(function() {
$collapslink = $(this);
//getting the next element
$collapscontent = $collapslink.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$collapscontent.slideToggle(500, function () {
//execute this after slideToggle is done
//change text of header based on visibility of content div
$collapslink.text(function () {
//change text based on condition
return $collapscontent.is(":visible") ? "Fermer" : "Lire la suite";
});
});
});
jQuery(function(){
jQuery(window).scroll(...);
});
修复:
您的处理程序不仅没有准备就绪,您实际上使用的是两个单独的页面加载处理程序。合并它,你应该全部设置: (请注意,为了简洁起见,我已经折叠了大型函数/变量声明。)
jQuery(document).ready(function() {
new Slideshowck(...); //Reduced these declarations
jQuery(window).scroll(...); //to take up less space
var ampzSettings = {...}; //in the answer
//Moved inside the document ready handler
jQuery(".collapslink").click(function() {
$collapslink = $(this);
//getting the next element
$collapscontent = $collapslink.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$collapscontent.slideToggle(500, function() {
//execute this after slideToggle is done
//change text of header based on visibility of content div
$collapslink.text(function() {
//change text based on condition
return $collapscontent.is(":visible") ? "Fermer" : "Lire la suite";
});
});
});
});
它可能适用于JSFiddle,因为默认情况下,JSFiddle将您的JavaScript包装在页面加载处理程序中。