我想使用 .each 和 .attr 函数来获取 div id。我知道该怎么做,但我想要做的是从 DOM 中获取具有特定类(假设为“编辑”类)的每个 div ID,然后将一些数据附加到该 div 中。
这就是我遇到的问题:我想将该特定 DIV 的 ID 放入 .append 内的 .each 循环中。例如:如果我想在 .append 中放置一个链接,我希望该链接为 http://www.website.com/index.php?id=1&div=DIV-ID
任何想法,我是个新手,所以你能提供代码吗例子。我在一定程度上理解 attr 和 .each 。
I want to use the .each and .attr function to get div ids. I know how to do that, but what I want to do is grab each div ID from the DOM with a certain class (let's say "edit" class) and .append some data into that div.
Here's what I am having trouble with: I want to put the ID of that particular DIV in the .each loop inside the .append. For example: if I wanted to put a link inside the .append, I would want the link to be http://www.website.com/index.php?id=1&div=DIV-ID
Any ideas, and I'm sorta a novice, so can you provide code example. I understand attr and .each though to an extent.
发布评论
评论(5)
http://jsfiddle.net/WrGLC/1/
为了将其保留在此页面上:
HTML:
JS:
http://jsfiddle.net/WrGLC/1/
For the sake of keeping it on this page:
HTML:
JS:
这是一个未经测试的解决方案,看起来应该可行。如果有多个类分配给
div
元素,它可能不起作用。Here's an untested solution that looks like it should work. It may not work if there are multiple classes assigned to the
div
elements.我猜一下。你想要这个吗?
I'll guess. You want this?
我认为我正确地解释了您的条件:
更新:@JohnHartsock 是正确的。这里不需要每种方法。我将调整我的代码如下:
I think I'm correctly interpreting your conditions:
UPDATE: @JohnHartsock is correct. No each method is needed here. I'd adjust my code as follows:
我认为没有必要对each() 进行jQuery 调用。使用 append() 您可以传递一个函数,该函数将代表选择器的当前迭代。
这是一个小提琴http://jsfiddle.net/WFd7k/
I don't believe an jQuery call to each() would be necessary. With append() you can pass a function where this will represent the current iteration of your selector.
Here is a fiddle http://jsfiddle.net/WFd7k/