7
<div id="calcwrapper">
    <img class="sugarcube" src="images/sugarcube.png" width="13" height="17">
    <div id="drinks">
        <a id="drink1" href=""><img src="images/drinkicon1.png" width="84" height="81"></a><div class="drink1"></div>
    </div>
</div>

在上面的示例中,只有一个饮料按钮,但我的代码包含八个按钮。每个都有一个对应的同名分类 div。我正在尝试做的是“动态”获取锚标记的 id (id="drink1") 以将克隆的糖立方图像 (img class="sugarcube" ...) 附加到等效的类名 div (class =“饮料1”)。我希望这听起来不会令人困惑。也许下面的不成功尝试会让你对我想要达到的目标有所了解。

尝试 1

$(".sugarcube").clone().attr("class", "sugarcube" + i).appendTo($(this).parent((".drink1").attr("class")));

尝试 2(尝试通过控制台找到可行的解决方案)

var className = $(this).attr("id");
console.log(className);

console.log($(this).parent().children("div").hasClass(className));

我搜索了 Google 和 StackOverflow 并没有找到任何代码示例。感谢您的帮助。

这是完整的 HTML 代码上下文...

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
    <script src="js/jquery-animate-css-rotate-scale.js"></script>
    <script>
        $(function() {
            $(".sugarcube").hide();
            var max = 8;
            function animateSugarcubes() {
                for (var i=1; i<=max; i++) {
                    $(".sugarcube" + i).css("position", "absolute");
                    $(".sugarcube" + i).css("top", Math.ceil(Math.random() * (50 - 20) + 20));
                    $(".sugarcube" + i).css("left", Math.ceil(Math.random() * (85 - 40) + 40));
                    $(".sugarcube" + i).hide();
                }
            }

            $("#drinks a").click(function() {

                for (var i=1; i<=max; i++) {
                    // Attempt 1
                    $(".sugarcube").clone().attr("class", "sugarcube" + i).appendTo($(this).parent().children($(this).attr("id")));

                    // Attempt 2 test code.
                    var className = $(this).attr("id");
                    console.log($(this).parent().children("div").hasClass(className));
                }

                return false;
            });
            animateSugarcubes();
        }); 
    </script>
</head> 
<body> 

<div id="calcwrapper">
    <img class="sugarcube" src="images/sugarcube.png" width="13" height="17">
    <div id="drinks">
        <a id="drink1" href=""><img src="images/drinkicon1.png" width="84" height="81"></a><div class="drink1"></div>
        <a id="drink2" href=""><img src="images/drinkicon2.png" width="84" height="81"></a><div class="drink2"></div>
        <a id="drink3" href=""><img src="images/drinkicon3.png" width="84" height="81"></a><div class="drink3"></div>
        <a id="drink4" href=""><img src="images/drinkicon4.png" width="80" height="81"></a><div class="drink4"></div>
        <a id="drink5" href=""><img src="images/drinkicon5.png" width="84" height="81"></a><div class="drink5"></div>
        <a id="drink6" href=""><img src="images/drinkicon6.png" width="84" height="81"></a><div class="drink6"></div>
        <a id="drink7" href=""><img src="images/drinkicon7.png" width="84" height="81"></a><div class="drink7"></div>
        <a id="drink8" href=""><img src="images/drinkicon8.png" width="84" height="81"></a><div class="drink8"></div>
    </div>
</div>

</body> 
</html> 

请注意,锚标记使用 id (id="drink1"),而 div 使用类 (class="drink1")

4

3 回答 3

4

如果我从字面上回答你的问题,那么我最终可能会得到这样的结果:

var drinksLinks = $("#drinks a"); // get list of all a tags inside drinks
drinksLinks.each(function() {     // perform function for each element
   var element = $(this);         // get jquery object for the current element
   var id = element.attr("id");   // get the id
   var div = element.find("." + id); // find the element with class matching the id

   $(".sugarcube").clone().appendTo(div);
});

但是,如果您试图找到一个特定元素,则应尽量避免按类引用事物。我的建议是实际上只是假设像drinks1 这样的div 总是紧挨着a 标签,那么你可以这样做:

var sugarcube = $(".sugarcube");
var drinksLinks = $("#drinks a"); // get list of all a tags inside drinks

drinksLinks.each(function() {
   var div = $(this).next();  // get the element next to the a tag
   sugarcube.clone().appendTo(div);
});

有几件事要记住:

  • 尝试使选择器尽可能窄,因为搜索 DOM 可能会很昂贵。
  • 在可能的情况下尝试通过 id 搜索 DOM,因为这比旧浏览器中的类本身要快
  • 如果您对同一个元素进行多次引用,请将其存储在一个变量中,就像我使用 sugarcube 元素一样。通过这种方式,您可以减少获得结果所需的页面搜索量

希望这可以帮助!

于 2012-12-07T16:23:19.230 回答
2

如果您使用带有标识符的元素,则使用“class”而不是“id”。“id”应该是唯一的。在给定的示例中,它用于 di 和 a 标签。

不是个好主意!<- 对不起,我以为有两个同名的 id

这对我有用。但有可能我没有正确理解你的问题

  $('#drink1').clone().attr('id','drink2').appendTo('#drinks')
于 2012-12-07T16:21:15.293 回答
1

像这样的东西怎么样:

var id = $(this).attr('id');
var targetDiv = $('.' + id);

Int 他的实例目标 div 将是类名与该 ID 匹配的 div

于 2012-12-07T16:24:01.430 回答