jQuery - 在一行中执行一段代码
我有一段代码:
$('#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将代码包装在函数中:
并在任意位置调用您的函数:
请参阅 Javascript 函数 教程以获取更多信息。
You can wrap the code in a function:
And call your function wherever you want:
See Javascript Functions tutorial for more info.
我可以建议它可以缩短:
它做同样的事情。您可以将其放入像这样的函数中:
并且只需调用 jQuery_ToggleSelected() 即可向这些元素添加/删除所需的样式。
Might I suggest it could be shortened:
Which does the same thing. You could put that in a function like so:
And just called
jQuery_ToggleSelected()
to add/remove the desired style to those elements.创建一个小插件来为您执行此操作,因为您似乎一直在这样做:
然后传递
jQuery
选择器数组来删除该类...瞧!
Create a small plugin that does this for you, since you seem to be doing it all the time:
Then pass
jQuery
an array of selectors to remove the class...And voila!
你可以使用一个对所有元素都相同的类并调用
或者如果你知道你正在调用什么元素,你可以删除所有 id 以 link 开头的元素(使用 div):
函数中调用所有内容
或者从我可能会的 最有可能按这个顺序使用它们,如果我不能/不想添加一个类,请使用 attr 选择器,如果完全不同,请使用一个函数。
尽管就代码看起来可以构建的方式而言,我更倾向于使用,但显然我不知道为什么你这样标记它们,而且很可能这是完全合乎逻辑的
You can either use a class that is the same for all and call
Or if you know what element you are calling you can remove all the ones with ids beginning with link (using div):
Or call everything from a function
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
将其放入函数中,然后调用该函数。
Put it in a function and then call the function instead.
我会给所有这些
#link_dwebsitedesign、#link_dhuisstijl
等元素一个公共类,例如class="link"
,然后只需使用.class
选择器 像这样:您可以在任何发现自己重复代码的地方执行此操作...如果如果您想要批量处理元素,请使用公共类来识别它们,然后对该类进行操作。
I would give all those
#link_dwebsitedesign, #link_dhuisstijl
, etc. elements a common class, for exampleclass="link"
, then just use.class
selector like this: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.
或者将其放入函数中
Or put it in a Function