JQuery 如何访问div
标记并附加它
你好,我正在尝试一些东西..我正在阅读Jquery遍历一些论坛的文档,这个网站之前问过与此主题相关的问题..实际上没有人回答我的问题,所以我会问..这是交易
<div class="entry">
week
<span>some text</span>
<p>DKK</p>
<input type="radio" name="red<% Response.Write(counter); %>" id="radio2" value="75" />
</div>
所以我使用了while 循环来创建一堆这些,所以我不必手动复制粘贴它们..asp 生成它们..现在你看到 p 部分,它说 DKK ..现在为了让我不用手动编写我想要的所有内容每个 div , p 都附加该 div 内的输入值,现在这是我对 Jquery 所做的
$(document).ready(function() {
$("input").each(function () {
var element_value = $(this).val();
$(this).closest("p").append(element_value + ",-");
});
});
所以我尝试做的是遍历每个输入并收集它们的值,然后找到最接近的 p 标签并附加它的值值,现在上面这个例子的正确结果是
<p>DKK 75,-<p>
因为输入值为 75 并且它已包含在附加函数中,-还要补充一点,此代码不起作用,但是如果我输入
$(this).parents("div.entry").append(element_value + ",-");
instead of
$(this).closest("p").append(element_value + ",-");
正确的结果会出现,但不完全是我的位置想要它们..希望有人可以帮助我,谢谢
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this websites previously asked questions relating this topic .. none actually answers my question so I'll ask instead .. here is the deal
<div class="entry">
week
<span>some text</span>
<p>DKK</p>
<input type="radio" name="red<% Response.Write(counter); %>" id="radio2" value="75" />
</div>
So I've used a while loop to create bunch of these so I don't have to manually copy paste them.. asp generates them .. now you see the p section it says DKK .. now in order for me not to write everything manually I'd like every divs , p to append with the value of input inside that div , now here is what I did with Jquery
$(document).ready(function() {
$("input").each(function () {
var element_value = $(this).val();
$(this).closest("p").append(element_value + ",-");
});
});
So what I tried to do is to go trough each input and collect their value, then find closest p tag and append it with its value, now the correct result for this example above would be
<p>DKK 75,-<p>
because input value is 75 and it has included in append function ,- add that as well, this code is not working, however if I put
$(this).parents("div.entry").append(element_value + ",-");
instead of
$(this).closest("p").append(element_value + ",-");
The correct results appear but not exactly where I want them .. hopefully someone can help me, thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您阅读closest()的文档,您会发现它只向上看,而不是向侧面看:
您可以执行此操作以从输入中查找先前的
:
或者更明确:
这将不依赖于
的哪一侧输入恰好打开。
If you read the documentation for closest() you will see it only looks upwards, not sideways:
You could do this to find the previous
<p>
from the input:Or a more explicit:
Which would then not depend on which side of the
<p>
the input happens to be on.试试这个:
Try this: