<div class="box">
<ul class="test">
<li><a href="#">test1</a></li>
<a class="add">test2</a>
</ul>
</div>
在我的HTML内容上面变得动态。我想在JQUERY中的test2
锚点链接中添加li标记。
我在这里试过 -
$('.box .test li:first-child').after('<li>');
但是这个li标签在错误的地方创建。但我希望我的输出应该是这样的 -
<div class="box">
<ul class="test">
<li><a href="#">test1</a></li>
<li><a class="add">test2</a></li>
</ul>
</div>
答案 0 :(得分:2)
最好和正确的方法是修复你的源HTML! a
元素中的ul
标记无效。所以你的第一个任务应该是获得正确的html输出。
如果不可能,您可以使用:not
选择器和wrap()
。因此,每个不是li
的子元素都将包含li
元素。这使它保持动态......
$('ul > *:not(li)').wrap('<li>');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
<ul class="test">
<li><a href="#">test1</a></li>
<a href="#" class="add">test2</a>
<strong>test3</strong>
</ul>
</div>
&#13;
答案 1 :(得分:0)
问题在于您的初始HTML,因为a
元素不能是ul
元素的直接子元素。尝试在初始渲染中修复此问题,并且以后不再使用javascript ,只是因为不同的浏览器会对无效的HTML片段采取不同的行为(自动修复它,删除它,你将其命名为.. )。
话虽如此,一般来说$('selector').wrap('<wrapperElement />')
是传统的jQuery用新创建的元素包装现有元素的方式。
答案 2 :(得分:-1)
下面的代码会将li标签包装在具有类add的标签周围。
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 15990: 'exception: this object is already an operator expression, and can't be used as a document expression (at 'then')' on server 192.168.1.127:27017. The full response is { "errmsg" : "exception: this object is already an operator expression, and can't be used as a document expression (at 'then')", "code" : 15990, "ok" : 0.0 }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:227)
at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:223)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:223)
at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:65)
at com.mongodb.Mongo.execute(Mongo.java:772)
at com.mongodb.Mongo$2.execute(Mongo.java:759)
at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
at com.mongodb.OperationIterable.forEach(OperationIterable.java:70)
at com.mongodb.AggregateIterableImpl.forEach(AggregateIterableImpl.java:117)
at threadpack.queryBtDates.main(queryBtDates.java:88)
$("a.add").wrap("<li>");
&#13;
$(document).ready(function(){
$("a.add").wrap("<li>");
});
&#13;
答案 3 :(得分:-3)
你实际上想要添加一个元素作为另一个元素的父元素,而是使用Jquery wrap函数:
$(".add:eq(0)").wrap("li")