如何让 jQuery .expander 在加载时调用,而不仅仅是在单击时调用?
我在使用 jQuery .expander 函数并在页面加载时调用它时遇到问题,我知道这与用 jQuery 动态填充的数据有关。
注意:看起来改变实际的 Ajax 函数不会是一个选择。
建议尝试 .live() ,它运行良好,但绑定 div.tags-hidden 时存在问题反复。
然后建议我使用变量“bound”并检查 div.tags-hidden 是否已先绑定,如果没有将变量设置为 true,这实际上会禁用任何其他 div.tags-hidden 的 .expander。
所以,实际上有两个问题,我希望得到任何建议:
1) .expander 函数只有在发生点击时才会调用
2) 用户首先点击的 div.tags-hidden是唯一具有 .expander 功能的。
下面的幻灯片希望能更好地说明这一点(参见下面的图片)。
幻灯片 1: 加载时,.expander 函数不会启动
幻灯片 2: .expander 仅在单击时发生
幻灯片 3:当前调用 .expander 的函数(代码见下文)。这包括 变量绑定=假。在使用 .live() 时建议这样做,但这会导致问题
幻灯片 4: 回到 UI,这两个模块包含大量标签来调用 .expander 函数,但只有点击的第一个 div.tags-hidden 才会得到这种处理。这是因为“bound”变量被设置为 false,这会强制扩展器处理为仅隐藏一个 div.tags,即第一个单击的。
总结一下: 如何让所有 div.tags-hidden 包含 .expander 函数 onload?
这是我当前使用的 jQuery:
<script type="text/javascript">
var bound = false;
$('div.tags-hidden').live('load click', function() {
if(!bound) {
$(this).find('p').expander({
slicePoint: 50,
expandText: '<img src="/images/layout/site/btn_expand.png" alt="Click to Expand Tags" title="Click to Expand Tags">',
collapseTimer: 0,
userCollapseText: '<img src="/images/layout/site/btn_collapse.png" alt="Click to Collapse Tags" title="Click to Collapse Tags">'
});
bound = true;
}
});
</script>
这是幻灯片:
非常感谢您对此提供的任何帮助。 米奇
I am having problems with jQuery .expander function and invoking it when the page loads and I know it has to do with the data being dynamically populated with jQuery.
NOTE: It looks like altering the actual Ajax functions is not going to be an option.
It was suggested to try .live() which has worked well but there are issues with div.tags-hidden being bound over and over again.
Then it was suggested that I use a variable "bound" and check to see if div.tags-hidden has been bound first and if not set the variable to true which in effect disables .expander for any other div.tags-hidden.
So, there are actually two issues which I would be grateful for any advice on:
1) The .expander function is not invoking until a click occurs
2) The div.tags-hidden that the user clicks first is the only one to have the .expander feature.
The slides below hopefully illustrate this better (see below for images).
SLIDE 1: On load the .expander function is not kicking in
SLIDE 2: The .expander only happens when a click happens
SLIDE 3: The current function to invoke .expander (see below for code). This includes the
variable bound=false. This was a recommended when using .live() but this is causing problems
SLIDE 4: Back to the UI, these two modules contain plenty of tags to invoke the .expander
function but only the first div.tags-hidden clicked is getting this treatment. This is due to the "bound" variable being set to false which forces the expander treatment to just one div.tags-hidden, the first one clicked.
To sum it up:
How can I get all the div.tags-hidden to contain the .expander function onload?
Here is the current jQuery I'm using:
<script type="text/javascript">
var bound = false;
$('div.tags-hidden').live('load click', function() {
if(!bound) {
$(this).find('p').expander({
slicePoint: 50,
expandText: '<img src="/images/layout/site/btn_expand.png" alt="Click to Expand Tags" title="Click to Expand Tags">',
collapseTimer: 0,
userCollapseText: '<img src="/images/layout/site/btn_collapse.png" alt="Click to Collapse Tags" title="Click to Collapse Tags">'
});
bound = true;
}
});
</script>
Here are the Slides:
Very thankful for any help with this.
Mitch
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试采取行动之前,请确保一切都已准备就绪。又名 $(document).ready();
Make sure that everything is ready for you before trying to act upon it. AKA $(document).ready();