jQuery 切换器行为不符合预期

发布于 2024-09-29 04:47:34 字数 1352 浏览 0 评论 0原文

我有一个正在使用的 jQuery 切换器。我可以让它同时切换多个 div 的可见性,但是一旦我实现了 next() 函数,唯一切换的是脚本的加号/减号部分,而不是 div 的可见性。这段代码中有什么值得注意的地方吗?

这是可行的,但会切换所有 div,而不仅仅是关闭一个:

jQuery(function(){
 jQuery(".toggleText").click(function(){
  jQuery(".hiddenText").slideToggle("fast");
        jQuery(this).html(function(i,html) {
            if (html.indexOf('More') != -1 ){
               html = html.replace('More','Less');
            } else {
               html = html.replace('Less','More');
            }
            return html;
        }).find('img').attr('src',function(i,src){
        return (src.indexOf('plus.gif') != -1)? 'minus.gif' :'plus.gif';
    });
});
});

这只会切换 more +/less - 而不是 .hiddenText div。 (两者唯一的区别是在第三行添加了 next() )。

jQuery(function(){
 jQuery(".toggleText").click(function(){
 jQuery(this).next(".hiddenText").slideToggle("none");
        jQuery(this).html(function(i,html) {
            if (html.indexOf('More') != -1 ){
               html = html.replace('More','Less');
            } else {
               html = html.replace('Less','More');
            }
            return html;
        }).find('img').attr('src',function(i,src){
        return (src.indexOf('plus.gif') != -1)? 'minus.gif' :'plus.gif';
    });
});
});

有什么想法吗?

PS:我使用 jQuery 而不是 $,因为与此代码所在的 CMS 发生冲突。在。

I have a jQuery toggler that I am fighting with. I can get it to toggle the visibility of multiple divs at once, but as soon as I implement the next() function, the only thing that toggles is the plus/minus portion of the script, not the visibility of the div. Anything stand out in this code?

This works, but toggles all divs instead of just the closes one:

jQuery(function(){
 jQuery(".toggleText").click(function(){
  jQuery(".hiddenText").slideToggle("fast");
        jQuery(this).html(function(i,html) {
            if (html.indexOf('More') != -1 ){
               html = html.replace('More','Less');
            } else {
               html = html.replace('Less','More');
            }
            return html;
        }).find('img').attr('src',function(i,src){
        return (src.indexOf('plus.gif') != -1)? 'minus.gif' :'plus.gif';
    });
});
});

This only toggles the more +/less - and not the .hiddenText div. (the only difference in these two is the addition of next() in the third line).

jQuery(function(){
 jQuery(".toggleText").click(function(){
 jQuery(this).next(".hiddenText").slideToggle("none");
        jQuery(this).html(function(i,html) {
            if (html.indexOf('More') != -1 ){
               html = html.replace('More','Less');
            } else {
               html = html.replace('Less','More');
            }
            return html;
        }).find('img').attr('src',function(i,src){
        return (src.indexOf('plus.gif') != -1)? 'minus.gif' :'plus.gif';
    });
});
});

Any ideas?

PS: I am using jQuery instead of the $ because of a conflict with the CMS this code lives. in.

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

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

发布评论

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

评论(2

那些过往 2024-10-06 04:47:34
jQuery(this).html(function(i,html) {

不要乱用 html()/innerHTML。它没有按照你的想法去做。

当您执行此操作时,您会要求浏览器将其当前文档模型序列化为 HTML 字符串。然后,您修改 HTML 并将其写回,此时浏览器必须销毁所有现有元素内容,并将其替换为从 HTML 字符串解析的新 DOM 节点。

除了缓慢且不可靠之外(因为您确实不知道浏览器将选择什么确切格式来序列化内容;它不会与您提供的原始 HTML 相同),这也必然会破坏 DOM 中无法转换为 HTML 标记字符串的所有信息。其中包括表单字段值、JavaScript 引用和事件处理程序。

因此,当您写回 HTML 时,您将丢失该标记中元素上的所有 click 事件,包括打开/关闭箭头上的事件。

忘记 HTML 字符串黑客吧。直接处理 DOM 元素,直接使用 More/Less 文本内容更改每个元素的 text()

jQuery(this).html(function(i,html) {

Don't futz with the html()/innerHTML. It is not doing what you think.

When you do this, you're asking the browser to serialise its current document model to an HTML string. You then fiddle with the HTML and write it back, at which point the browser has to destroy all the existing element content and replace it with new DOM nodes parsed from the HTML string.

Apart from being slow and unreliable (because you really don't know what exact format the browser will choose to serialise the content in; it won't be the same as the original HTML you fed it), this also necessarily destroys all information in the DOM that cannot be converted to a string of HTML markup. That includes form field values, JavaScript references, and event handlers.

So, when you write the HTML back, you will have lost all click events on element in that markup, including those on the open/close arrows.

Forget HTML string hacking. Work directly on the DOM elements, changing the text() of each element with the More/Less text content directly.

梦归所梦 2024-10-06 04:47:34

从你写的方式来看,

jQuery(this).next(".hiddenText")

我猜你想在 jQuery(this) 之后找到下一个 .hiddentText ?这不是 .next() 的工作原理。我不会找到下一个同级,而是通过可选选择器过滤它。

则可以使用。

jQuery(this).parent().children(".hiddenText")

如果 jQuery(this) 的父级中只有一个 .hiddentText

From the way you write

jQuery(this).next(".hiddenText")

I'm guessing that you want to find the next .hiddentText after jQuery(this)? That's not how .next() works. I't will find the next sibling, but filter it by the optional selector.

You could possibly use

jQuery(this).parent().children(".hiddenText")

if there is only one .hiddentText in the parent of jQuery(this).

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