当前位置:嗨网首页>书籍在线阅读

50-解决方案

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

1.12.2 解决方案

使用 replaceWith() 方法,可以选择一组DOM元素进行替换。在下面的代码示例中,使用 replaceWith() 方法,将 class 属性为 remove 的所有 <li> 元素替换为新的DOM结构:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<ul>
<li class='remove'>name</li>
<li>name</li>
<li class='remove'>name</li>
<li class='remove'>name</li>
<li>name</li>
<li class='remove'>name</li>
<li>name</li>
<li class='remove'>name</li>
</ul>
<script type="text/JavaScript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/JavaScript">
 jQuery('li.remove').replaceWith('<li>removed</li>');
</script>
</body>
</html>

添加到DOM中的新DOM结构作为一个字符串参数传递给 relaceWith() 方法。在例子中,所有 <li> 元素(包括子元素)都被新的结构 <li>remove</li> 替代。