【问题标题】:jquery each function not working in sharepointjquery每个函数在sharepoint中不起作用
【发布时间】:2015-03-21 07:04:05
【问题描述】:

为了将所有 excel 应用程序 Web 部件集中在一个 sharepoint 2013 sp1 页面上,我想我可以使用 jquery 来做到这一点。我尝试了以下代码来处理水平居中。不幸的是,该函数似乎永远不会被调用。

我希望有 9 个 alert() 因为页面上有 9 个 div 匹配选择器。换句话说,有 9 个 div 具有 class="ewr-sheetcontainer ewr-grdblkcontainer ewa-scrollbars"。是的,它有 3 个类,css 允许这种类型的选择,但 jquery 可能不允许。

此代码嵌入在页面中,并且为网站集启用了 jquery。

是否有人发现脚本、函数或调用方式存在问题?目标是调整所选 div 的 left 属性。

<script type="text/javascript">

$(document).ready(function() { 
    $("div.ewr-sheetcontainer ewr-grdblkcontainer ewa-scrollbars").each( function () {
        this.css("position","absolute");
        this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + 
                                                    $(window).scrollLeft()) + "px");
        alert("hello");
        return this;
    } );

}); 

&lt;/script&gt;

【问题讨论】:

    标签: jquery


    【解决方案1】:

    试试这个,你似乎错过了指定类的点。

    $(document).ready(function() { 
        $("div.ewr-sheetcontainer .ewr-grdblkcontainer .ewa-scrollbars").each( function () {
            this.css("position","absolute");
            this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
            alert("hello");
            return this;
        });
    
    }); 
    

    【讨论】:

    • 嗯,看起来 jquery 只允许一个类选择器。那个,或者我不知道如何选择具有多个类的 div。我试过你的方法失败了。然后我削减到一个类,如 $("div.ewr-sheetcontainer").each(....) 并且迭代工作。如果 jquery 有这个限制,那么 css 或内联 html 提供更灵活的选择。我试图通过使用更苛刻的标准来限制选择的 div。谢谢您的帮助。我仍然会研究 jquery 选择器 - 我不是 jquery 人。
    猜你喜欢
    • 2011-10-10
    • 2016-09-20
    • 2013-04-21
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 2013-08-04
    • 2016-09-14
    相关资源
    最近更新 更多