if 子句与 img 生成有关的问题
脚本如下;
$("#Results a").live('dblclick', function(event){
var src = $(this).attr("href");
if (event.type === 'dblclick') {
if ($(this).hasClass(".A")) {
$('<img />', {'src': src,'class': 'Box'}).wrap('<div class="Packet" />').parent().insertAfter($('.Spot_A').parent());
} else if ($(this).hasClass(".B")) {
$('<img />', {'src': src,'class': 'Box'}).wrap('<div class="Packet" />').parent().insertAfter($('.Spot_B').parent());
}
}
});
我双击链接标签来创建一个 img 元素,该元素使用图像 src 的链接的 href,并根据链接元素所具有的类将此 img 放置在特定的位置。 img 元素创建部分工作完美。但是,我现在尝试根据链接标签的类别对各种图像进行“排序”。这就是我遇到麻烦的地方。对我来说,它应该如何进行似乎是显而易见的,但它不起作用。
我想,它应该简单地是:当我双击一个链接时,如果这个(链接)有类__,请在此处创建一个img。如果是 diff 类,则在此处创建一个 img。请注意,链接在任何时候都不会被删除。
.parent/insertafter/parent 部分是必需的,因为它是确定层次结构内放置方式的一部分。 Spot_A/B是起点,这是排序的关键。
Script is as follows;
$("#Results a").live('dblclick', function(event){
var src = $(this).attr("href");
if (event.type === 'dblclick') {
if ($(this).hasClass(".A")) {
$('<img />', {'src': src,'class': 'Box'}).wrap('<div class="Packet" />').parent().insertAfter($('.Spot_A').parent());
} else if ($(this).hasClass(".B")) {
$('<img />', {'src': src,'class': 'Box'}).wrap('<div class="Packet" />').parent().insertAfter($('.Spot_B').parent());
}
}
});
I double click a link tag to create an img element that uses the href of the link for the src of the image, and places this img somewhere specific, based on the class the link element had. The img element creation part works perfectly. However, i'm now trying to 'sort' the various imgs based on what class the link tag had. This is where i'm having trouble. To me, it seems dead obvious how it should go, but it fails to work.
I figured, it should simply be: When i double click a link, if this (the link) has class __, create an img HERE. If a diff class, then create an img HERE. Note that the links aren't being deleted at any point.
The .parent/insertafter/parent part is necessary because its a part of how the placement within the hierarchy is determined. Spot_A/B is the starting point, and this is the key for the sorting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.hasClass()
采用类的名称,而不是类选择器。删除句号。对此进行完整的清理处理:
this
,将$(this)
存储在局部变量中event.type === 'dblclick'
if-else
情况可以用两个集合写得更简洁三元运算符$()
创建新元素时).hasClass()
takes the name of a class, not a class selector. Remove the periods.Giving this the full cleanup treatment:
this
, store$(this)
in a local variableevent.type === 'dblclick'
if-else
case can be written more tersely with two sets of ternary operators$()
to create new elements)