2011-05-31 55 views
0

我写了这个:jQuery的HTML问题

<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
    <script> 
     $("div.productInfo").wrap("<div id='productDetails'></div>"); 
    </script> 
</head> 

<body> 
    <div class="productInfo">Whatever.</div> 
</body> 

而且它没有工作? 谢谢。

回答

3

你的元素,当你的脚本运行还没有被渲染...试试这个:

<script> 
    $(document).ready(function(){ 
     $("div.productInfo").wrap("<div id='productDetails' />"); 
    }); 
</script> 
+0

它的工作,但我的一些图像删除我不知道为什么?谢谢 – 2011-06-01 00:51:22

+0

我猜你的“productDetails”div也应该有一个“productInfo”类,以便它继承相同的样式并且不会影响你的布局 – Trey 2011-06-01 00:53:17

1

看起来你忽略了的document.ready:

<script> 
    $(document).ready(function(){ 
     $("div.productInfo").wrap("<div id='productDetails' />"); 
    }); 
</script> 
+0

它工作但我的一些图像被移除我不知道为什么?谢谢 – 2011-06-01 00:50:56

1

放置脚本元素在页面的底部,并使用ready handler

<!DOCTYPE html> 

<html> 
<head> 
    <title>A valid page</title> 
</head> 
<body> 
<div class="productInfo">Whatever.</div> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script> 
    $(function() { 
     $('div.productInfo').wrap('<div id="productDetails"></div>'); 
    }); 
</script> 
</body> 
</html> 
+0

它的工作,但我的一些图像删除我不知道为什么?谢谢 – 2011-06-01 00:54:58