【问题标题】:jquery assigning click function from a pluginjquery从插件分配点击功能
【发布时间】:2010-11-15 17:45:15
【问题描述】:

这是一个我正在更新的旧项目,所以不是最漂亮的编码。

我正在尝试根据我的特定需求调整插件,有问题的插件是一个简单的可折叠面板插件。

我希望能够根据分配给 div 的 rel 为每个面板设置不同颜色的样式。

它实际上是生成css和div代码的php代码,然后我调用jquery来应用可折叠面板插件。

它的样式一切正常,但点击功能不起作用,任何指针都会最有帮助:我想我需要在某处应用 .delegate() 但不知道如何。

网址:http://www.reelfilmlocations.co.uk/NEW%20Search/fullsearch_rework.php 要获得测试结果:单击选择自治市镇选择器上方的[选择全部],然后单击搜索按钮。

创建面板的代码:

//all this is wrapped in a php loop:
<style type="text/css">
            .C_<?php echo $myCurrentBorough ?>_color{
               color:<?php echo $row_rs_myBoroughs['color']; ?>;
               font-family: Arial, Helvetica, sans-serif;
               font-size:12px;
               letter-spacing:1px;
               text-transform:uppercase; 
               font-weight: bold;   
            }
            .collapsibleContainer<?php echo $myCurrentBorough ?>{
                     border: solid 0px <?php echo $row_rs_myBoroughs['color']; ?>;
                }

                .collapsibleContainerTitle<?php echo $myCurrentBorough ?>{
                     cursor:pointer;
                     color:#000000;
                     background-color:<?php echo $row_rs_myBoroughs['color']; ?>
                }

                .collapsibleContainerTitle div<?php echo $myCurrentBorough ?>{
                     padding-top:5px;
                     padding-left:10px;
                     background-color:<?php echo $row_rs_myBoroughs['color']; ?>;
                     color:#607882;
                }

                .collapsibleContainerContent<?php echo $myCurrentBorough ?>{
                     padding: 10px;
                     background-color:<?php echo $row_rs_myBoroughs['color']; ?>;
                }

         </style>
        <?php if($boroughCounter > 0){?>

             <div class="collapsibleContainer<?php echo $myCurrentBorough; ?>" tabindex="<?php echo $myCurrentBorough; ?>" title="Locations Found In <?php echo $row_rs_myBoroughs['Borough_Name']; ?>" rel="<?php echo $myCurrentBorough; ?>">
            MY DIV CONTENT GOES HERE
               </div>
               <script type="text/javascript">
                        $(document).ready(function() {
                            $(".collapsibleContainer<?php echo $myCurrentBorough; ?>").collapsiblePanel();
                        });
                    </script>
                    <?php } //end check to see if locations exist in borough?>

jquery 插件:

    (function($) {
    $.fn.extend({
        collapsiblePanel: function() {
            // Call the ConfigureCollapsiblePanel function for the selected element
            return $(this).each(ConfigureCollapsiblePanel);
        }
    });

})(jQuery);

function ConfigureCollapsiblePanel() {
    $(this).addClass("ui-widget");
    // Wrap the contents of the container within a new div.
    $(this).children().wrapAll("<div class='collapsibleContainerContent"+$(this).attr("rel")+" ui-widget-content' rel='"+$(this).attr("rel")+"'></div>");

    // Create a new div as the first item within the container.  Put the title of the panel in here.
    $("<div class='collapsibleContainerTitle"+$(this).attr("rel")+" ui-widget-header'><div>" + $(this).attr("title") + "</div></div>").prependTo($(this));

    // Assign a call to CollapsibleContainerTitleOnClick for the click event of the new title div.
    $(".collapsibleContainerTitle"+$(this).attr("rel")+"", this).click(CollapsibleContainerTitleOnClick());
}

function CollapsibleContainerTitleOnClick(myID) {
    // The item clicked is the title div... get this parent (the overall container) and toggle the content within it.
    $(".collapsibleContainerContent"+$(this).attr("rel")+"", $(this).parent()).slideToggle();
}

【问题讨论】:

    标签: jquery jquery-plugins click


    【解决方案1】:

    click 方法正在调用函数,而不是传递对函数的引用。去掉(),事件就会触发。

    当前代码: $(".collapsibleContainerTitle"+$(this).attr("rel")+"", this).click(CollapsibleContainerTitleOnClick());

    固定代码: $(".collapsibleContainerTitle"+$(this).attr("rel")+"", this).click(CollapsibleContainerTitleOnClick);

    【讨论】:

    • 感谢您的回复,我尝试了您建议的代码,但单击时仍然无法打开/关闭面板
    • 我在点击中添加了一个console.log,右侧的父级显示在控制台中,为什么它不起作用似乎很奇怪
    • $(".collapsibleContainerContent"+$(this).parent().parent().attr("rel")).toggle()
    • 第一个问题是选择器没有找到正确的 DOM 元素。第二个问题是 slideToggle() 不起作用...我不确定出了什么问题...但是 toggle() 有效。
    • 您使用的是旧版本的 jquery 1.3.2...我不知道这是否有所作为。您可以尝试升级到 1.4.X 看看是否有帮助。你在用 firebug,我用来追踪 bug 的工具吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2012-12-30
    • 2014-03-05
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多