如何让 jQuery .expander 在加载时调用,而不仅仅是在单击时调用?

发布于 2024-11-19 04:17:39 字数 1805 浏览 2 评论 0原文

我在使用 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>

这是幻灯片:

Slide 1

幻灯片 2

幻灯片 3

Slide 4

非常感谢您对此提供的任何帮助。 米奇

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:

Slide 1

Slide 2

Slide 3

Slide 4

Very thankful for any help with this.
Mitch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2024-11-26 04:17:39

在尝试采取行动之前,请确保一切都已准备就绪。又名 $(document).ready();

<script type="text/javascript">
$(document).ready(function(){

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>

Make sure that everything is ready for you before trying to act upon it. AKA $(document).ready();

<script type="text/javascript">
$(document).ready(function(){

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文