将 href 点击函数放入 if-else 语句中
我有一个函数
$("a[href*=/category/movies/]").click(function() {
so.addVariable('stretching','fill');
so.write('mediaspace');
});
,我想将其转换为 if...else 语句。
如何定位 div#header_playlist_categories 内的链接 并应用 else 语句,如果链接指向“/categories/movies/”以外的其他内容?
I've got this function
$("a[href*=/category/movies/]").click(function() {
so.addVariable('stretching','fill');
so.write('mediaspace');
});
that I want to turn into an if...else statement.
How do I target links inside div#header_playlist_categories
and apply an else statement, if links point to other than '/categories/movies/' ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您想要做的是处理
div#header_playlist_categories
中的a
元素的所有点击。然后,如果链接的href
属性包含/category/movies/
,您需要执行一项操作,如果不包含,则执行另一项操作。如果是这样,您可以使用delegate()
很好地做到这一点:I think what you want to do is to handle all clicks on
a
elements withindiv#header_playlist_categories
. You then want to do one thing if the link'shref
attribute contains/category/movies/
and another if it does not. If so, you can do this nicely usingdelegate()
:您可以像这样使用
:not()
来获取 < em>其他链接:或者更便宜一点,检查循环:
You can use
:not()
like this to get the other links:Or a bit cheaper, check in the loop:
您应该将该事件委托给您的
div# header_playlist_categories
。在回调函数中,访问所单击链接的“href”属性,并根据其值对处理程序进行分支。
You should delegate that event to your
div#header_playlist_categories
.Inside your callback function, access the "href" attribute of the link that was clicked and branch your handler based on its value.