我在页面中有一个可变数量的div,我需要创建一个增量ID来添加到div。这已经工作了,现在,在div中我还有一个按钮需要与父亲具有相同的ID ... 这就是我所做的,但我不知道它为什么不起作用。
HTML
<div class="article-container">
This is the text of article 1<br/>
With some html tags like this <span>SPAN</span>, this <b>Bold text</b>, some <i>Italic</i> and some <a href="www.google.co.uk">Links</a>
</div>
<button class="btn">
Copy Article
</button>
<hr>
<div class="article-container">
This is the text of article 1<br/>
With some html tags like this <span>SPAN</span>, this <b>Bold text</b>, some <i>Italic</i> and some <a href="www.google.co.uk">Links</a>
</div>
<button class="btn">
Copy Article
</button>
的jQuery
var parentDivs = $(".article-container");
$(parentDivs).each(function (i) {
var index = i + 1;
$(this).attr('id', index);
$('button.btn', this ).attr('id', index);
})
修改 正如下面几次提到的那样,按钮不在div中,所以这就是为什么没有拿起id(我将改变前缀,感谢你的注意)
我在div中移动了按钮,现在它正常工作。 感谢
答案 0 :(得分:2)
有两个问题。
所以要么移动div中的按钮,要么使用next()
选择器。
例如
<button class="btn" id="btn">Button 1</button>
<button class="btn" id="btn">Button 2</button>
以上$(“#btn”)将始终返回第一个按钮。
答案 1 :(得分:0)
试试这个。 var parentDivs = $(“。article-container”);
$(parentDivs).each(function (i) {
var index = i + 1;
$(this).attr('id', index);
$(this).find('button.btn').attr('id', "btn"+index);
})
修改强>
你提到按钮是文章容器div的子项吗?所以我也纠正了HTML。这应该有用。
var parentDivs = $(".article-container");
$(parentDivs).each(function (i) {
var index = i + 1;
$(this).attr('id', index);
$(this).find('button.btn').attr('id', "btn"+index);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="article-container">
This is the text of article 1<br/>
With some html tags like this <span>SPAN</span>, this <b>Bold text</b>, some <i>Italic</i> and some <a href="www.google.co.uk">Links</a>
<button class="btn">
Copy Article
</button>
</div>
<hr>
<div class="article-container">
This is the text of article 1<br/>
With some html tags like this <span>SPAN</span>, this <b>Bold text</b>, some <i>Italic</i> and some <a href="www.google.co.uk">Links</a>
<button class="btn">
Copy Article
</button>
</div>
答案 2 :(得分:0)
您需要将parentDivs
与.next()
一起使用以定位正确的按钮,而不是重复使用ID。
var parentDivs = $(".article-container");
$(parentDivs).each(function (i) {
var index = i + 1;
$(this).attr('id', 'a'+index);
$(this).next('button.btn').attr('id', 'b'+index);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="article-container">
This is the text of article 1<br/>
With some html tags like this <span>SPAN</span>, this <b>Bold text</b>, some <i>Italic</i> and some <a href="www.google.co.uk">Links</a>
</div>
<button class="btn">
Copy Article
</button>
<hr>
<div class="article-container">
This is the text of article 1<br/>
With some html tags like this <span>SPAN</span>, this <b>Bold text</b>, some <i>Italic</i> and some <a href="www.google.co.uk">Links</a>
</div>
<button class="btn">
Copy Article
</button>
&#13;
请注意,我还在ID前面加上字母字符,因为出于各种原因,数字ID存在问题。
答案 3 :(得分:0)
更改以下行
$(this).next('button.btn').attr('id', index);
为:
def evolve(m_all, pp_all, pv_all, tf, dt):
"""
Evolves the initial positions and initial velocites of particles for a given time step and total time, then
updates their positions.
Parameters
----------
m_all : np.ndarray
1-D array containing masses for all particles, length N, where
N is the number of particles.
pp_all : np.ndarray
2-D array containing (x, y, z) positions for all particles.
Shape is (N, 3) where N is the number of particles.
pv_all : np.ndarray
2-D array containing (x, y, z) velocities for all particles.
Shape is (N, 3) where N is the number of particles.
tf : float
The total time to evolve the system.
dt : float
Evolve system for time dt.
Returns
-------
partpos : N-D array
N-D array with the updated particle postions for each time step, dt.
partvel : N-D array
N-D array with the updated particle velocities for each time step, dt.
"""
partpos = np.zeros((10000, 3)) # create empty array with 10000 elements
partvel = np.zeros((10000, 3)) # create empty array with 10000 elements
t = 0 # initial time
i = 0
while t < tf:
new_positions, new_velocities = \
evolve_particles(pp_all, pv_all, m_all, dt)
t += dt # add time step
partpos[i] = new_positions[i]
partvel[i] = new_velocities[i]
i += 1
pp_all = new_positions
pv_all = new_velocities
return partpos, partvel
工作示例: