如何附加ul和div id与jquery附加ul类相同?

时间:2014-09-01 19:21:28

标签: jquery html

如何附加ul和div id与jquery附加ul类相同?但我尝试但是我没有成功请发给我原因

<div class="mainrow">
<div class="row">
<div class="cated1" id="cate1">cate1</div>
<div class="cated1" id="cate2">cate2</div>
</div>
<div class="appendrow">
<ul class="cate1">
<li>test</li>
<li>test</li>
</ul>  
<ul class="cate2">
<li>test2</li>
<li>test2</li>
</ul>    
</div>    
</div> 

<script type="text/javascript">
$(document).ready(function() {
var idrow = $(".mainrow .cated1").attr("id");
$("ul").each(function(index, element) {
alert($(this).appendTo(idrow));
});

});
</script>

1 个答案:

答案 0 :(得分:0)

您可以这样使用attr()

$(document).ready(function () {

    $("ul").each(function (index, element) {
        // $(this) will give current ul reference and .attr("class") will give its class name
        alert($(this).appendTo($("div#" + $(this).attr("class"))));
    });

});

FIDDLE DEMO:

http://jsfiddle.net/ehsansajjad465/f3gzks0p/