简体   繁体   English

我可以在JQuery中整合函数调用吗?

[英]Can i consolidate function calls in JQuery?

I have following and would like to be able to use one function only preferably... 我有以下内容,并希望能够最好只使用一个功能...

$(document).ready(function(){

$("#home_p1").editInPlace({
        url: './include_eip_77994477/eip.php',
    });

$("#home_p2").editInPlace({
        url: './include_eip_77994477/eip.php',
    });

$("#home_p3").editInPlace({
        url: './include_eip_77994477/eip.php',
    });
});

This could go to 15 or 20 instances on some pages...is it possible? 这可能会在某些页面上发生15或20个实例......是否可能?

You can combine the different elements with a comma: 您可以使用逗号组合不同的元素:

$("#home_p1, #home_p2, #home_p3").editInPlace({
    url: './include_eip_77994477/eip.php',
});

Or you can add a class to all your elements and use a class selector: 或者您可以为所有元素添加一个类并使用类选择器:

$(".someClass").editInPlace({
    url: './include_eip_77994477/eip.php',
});

jQuery selectors jQuery选择器

2/3 options ... 2/3选项......

1) combine the selectors : 1)组合选择器:

$("#home_p1,#home_p1").editInPlace({
    url: './include_eip_77994477/eip.php',
});

2) use a class : 2)使用一个类:

$(".yourclass").editInPlace({
    url: './include_eip_77994477/eip.php',
});

3) If your IDs match a pattern as in your example : 3)如果您的ID与示例中的模式匹配:

$("[id^=home_]").editInPlace({
    url: './include_eip_77994477/eip.php',
});

You could add a class to your #home_px elements and target it using the class? 你可以在你的#home_px元素中添加一个类并使用该类来定位它吗? The urls are all the same in your example, but if they were different you could use data. 您的示例中的网址都是相同的,但如果它们不同,您可以使用数据。

$(".element").editInPlace({
    url: $(this).data('url'),
});

<div class="element" data-url="./include_eip_77994477/eip.php"></div>

Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM