jQuery - 在一行中执行一段代码

发布于 2024-09-25 12:40:03 字数 631 浏览 3 评论 0原文

我有一段代码:

                $('#link_dwebsitedesign a').removeClass('selected');
            $('#link_dhuisstijl a').removeClass('selected');
            $('#link_dhosting a').removeClass('selected');
            $('#link_dwordpress a').removeClass('selected');
            $('#link_dseo a').removeClass('selected');
            $('#link_dcms a').removeClass('selected');
            $('#link_dslicen a').removeClass('selected');
            $('#link_dwebshop a').removeClass('selected');

并且它需要在我的脚本中执行多次。

是否有一个选择器或任何东西可以让我在一行中执行这段代码?

示例:$('blockofcode').execute();

I have a block of code:

                $('#link_dwebsitedesign a').removeClass('selected');
            $('#link_dhuisstijl a').removeClass('selected');
            $('#link_dhosting a').removeClass('selected');
            $('#link_dwordpress a').removeClass('selected');
            $('#link_dseo a').removeClass('selected');
            $('#link_dcms a').removeClass('selected');
            $('#link_dslicen a').removeClass('selected');
            $('#link_dwebshop a').removeClass('selected');

And it needs to be executed several times in my script.

Isn't there a selector or anything so i can execute this block of code in one line?

Example: $('blockofcode').execute();

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

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

发布评论

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

评论(7

怪我太投入 2024-10-02 12:40:03

您可以将代码包装在函数中:

     function mycode(){
        $('#link_dwebsitedesign a').removeClass('selected');
        $('#link_dhuisstijl a').removeClass('selected');
        $('#link_dhosting a').removeClass('selected');
        $('#link_dwordpress a').removeClass('selected');
        $('#link_dseo a').removeClass('selected');
        $('#link_dcms a').removeClass('selected');
        $('#link_dslicen a').removeClass('selected');
        $('#link_dwebshop a').removeClass('selected');
     }

并在任意位置调用您的函数:

mycode();

请参阅 Javascript 函数 教程以获取更多信息。

You can wrap the code in a function:

     function mycode(){
        $('#link_dwebsitedesign a').removeClass('selected');
        $('#link_dhuisstijl a').removeClass('selected');
        $('#link_dhosting a').removeClass('selected');
        $('#link_dwordpress a').removeClass('selected');
        $('#link_dseo a').removeClass('selected');
        $('#link_dcms a').removeClass('selected');
        $('#link_dslicen a').removeClass('selected');
        $('#link_dwebshop a').removeClass('selected');
     }

And call your function wherever you want:

mycode();

See Javascript Functions tutorial for more info.

小巷里的女流氓 2024-10-02 12:40:03

我可以建议它可以缩短:

$('#link_dwebsitedesign a, #link_dhuisstijl a, #link_dhosting a, #link_dwordpress a, #link_dseo a,#link_dcms a, #link_dslicen a, #link_dwebshop a').toggleClass('selected');

它做同样的事情。您可以将其放入像这样的函数中:

function jQuery_ToggleSelected()  
{  
    $('#link_dwebsitedesign a, #link_dhuisstijl a, #link_dhosting a, #link_dwordpress a, #link_dseo a,#link_dcms a, #link_dslicen a, #link_dwebshop a').toggleClass('selected');    
}  

并且只需调用 jQuery_ToggleSelected() 即可向这些元素添加/删除所需的样式。

Might I suggest it could be shortened:

$('#link_dwebsitedesign a, #link_dhuisstijl a, #link_dhosting a, #link_dwordpress a, #link_dseo a,#link_dcms a, #link_dslicen a, #link_dwebshop a').toggleClass('selected');

Which does the same thing. You could put that in a function like so:

function jQuery_ToggleSelected()  
{  
    $('#link_dwebsitedesign a, #link_dhuisstijl a, #link_dhosting a, #link_dwordpress a, #link_dseo a,#link_dcms a, #link_dslicen a, #link_dwebshop a').toggleClass('selected');    
}  

And just called jQuery_ToggleSelected() to add/remove the desired style to those elements.

痴骨ら 2024-10-02 12:40:03

创建一个小插件来为您执行此操作,因为您似乎一直在这样做:

(function($) {
  $.fn.removeSelected = function() {
    return $(this).each(function() {
         $('a', this).removeClass('selected');
     });   
  });
})(jQuery);

然后传递 jQuery 选择器数组来删除该类...

var links_array = ['#link_dwebsitedesign', '#link_dhuisstijl', '#link_dhuisstijl', '#link_dwordpress', '#link_dseo', '#link_dcms', '#link_dslicen', '#link_dwebshop'];

function removeLinks(links) {
    $(links.join(', ')).removeSelected();
}
removeLinks(links_array);

瞧!

Create a small plugin that does this for you, since you seem to be doing it all the time:

(function($) {
  $.fn.removeSelected = function() {
    return $(this).each(function() {
         $('a', this).removeClass('selected');
     });   
  });
})(jQuery);

Then pass jQuery an array of selectors to remove the class...

var links_array = ['#link_dwebsitedesign', '#link_dhuisstijl', '#link_dhuisstijl', '#link_dwordpress', '#link_dseo', '#link_dcms', '#link_dslicen', '#link_dwebshop'];

function removeLinks(links) {
    $(links.join(', ')).removeSelected();
}
removeLinks(links_array);

And voila!

生活了然无味 2024-10-02 12:40:03

你可以使用一个对所有元素都相同的类并调用

$('.link a').removeClass('selected');

或者如果你知道你正在调用什么元素,你可以删除所有 id 以 link 开头的元素(使用 div):

   $('div[id^=link_] a').removeClass('selected');

函数中调用所有内容

function removeLinkSelects(){
    $('#link_dwebsitedesign a').removeClass('selected');
    $('#link_dhuisstijl a').removeClass('selected');
    $('#link_dhosting a').removeClass('selected');
    $('#link_dwordpress a').removeClass('selected');
    $('#link_dseo a').removeClass('selected');
    $('#link_dcms a').removeClass('selected');
    $('#link_dslicen a').removeClass('selected');
    $('#link_dwebshop a').removeClass('selected');
}
removeLinkSelects();

或者从我可能会的 最有可能按这个顺序使用它们,如果我不能/不想添加一个类,请使用 attr 选择器,如果完全不同,请使用一个函数。
尽管就代码看起来可以构建的方式而言,我更倾向于使用,但显然我不知道为什么你这样标记它们,而且很可能这是完全合乎逻辑的

You can either use a class that is the same for all and call

$('.link a').removeClass('selected');

Or if you know what element you are calling you can remove all the ones with ids beginning with link (using div):

   $('div[id^=link_] a').removeClass('selected');

Or call everything from a function

function removeLinkSelects(){
    $('#link_dwebsitedesign a').removeClass('selected');
    $('#link_dhuisstijl a').removeClass('selected');
    $('#link_dhosting a').removeClass('selected');
    $('#link_dwordpress a').removeClass('selected');
    $('#link_dseo a').removeClass('selected');
    $('#link_dcms a').removeClass('selected');
    $('#link_dslicen a').removeClass('selected');
    $('#link_dwebshop a').removeClass('selected');
}
removeLinkSelects();

I'd probably be most likely to use those in that order, if I couldn't/didn't want to add a class use the attr selector and if that was wildly different use a function.
Although in terms of the way the code looks like it could be structured I'd be more inclined to use but obviously I don't know why you've marked them up like that and most likely it's perfectly logical

云胡 2024-10-02 12:40:03

将其放入函数中,然后调用该函数。

Put it in a function and then call the function instead.

谁把谁当真 2024-10-02 12:40:03

我会给所有这些 #link_dwebsitedesign、#link_dhuisstijl 等元素一个公共类,例如 class="link",然后只需使用 .class 选择器 像这样:

$(".link a").removeClass("selected");

您可以在任何发现自己重复代码的地方执行此操作...如果如果您想要批量处理元素,请使用公共类来识别它们,然后对该类进行操作。

I would give all those #link_dwebsitedesign, #link_dhuisstijl, etc. elements a common class, for example class="link", then just use .class selector like this:

$(".link a").removeClass("selected");

You can do this anywhere you find yourself repeating code...if you want to handle elements in a batch, use a common class to identify them, then operate on that class.

硬不硬你别怂 2024-10-02 12:40:03
$('#link_dwebsitedesign a , #link_dhuisstijl a, #link_dhosting a, #link_dwordpress a, #link_dseo a, #link_dcms a, #link_dslicen a, #link_dwebshop a').removeClass('selected');

或者将其放入函数中

$('#link_dwebsitedesign a , #link_dhuisstijl a, #link_dhosting a, #link_dwordpress a, #link_dseo a, #link_dcms a, #link_dslicen a, #link_dwebshop a').removeClass('selected');

Or put it in a Function

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