简体   繁体   English

选择同级div jQuery的跨度

[英]Select span of sibling div jquery

I am using jQuery selectors, and I need to be able to find each span individually that contains a dollar amount. 我正在使用jQuery选择器,并且我需要能够找到包含美元金额的每个范围。 For example, I'd like to find & change the first span to $15 and the third span to $40. 例如,我想查找并将第一个span更改为$ 15,将第三个span更改为$ 40。 I have put in class names in my example, but in my problem I do not know the class names, and I do not know which span the 2nd dollar amount is in. There also could be more or less div's and spans. 我在示例中输入了类名,但是在我的问题中,我不知道类名,也不知道第二元金额位于哪个跨度中。div或跨度也可能会有所不同。

This happens to be in a form, and I find the first dollar amount like this: 这恰好是一种形式,我发现第一个美元金额是这样的:

$("form[action='/cart'] span:contains('$'):first).replaceWith("$15");

I have the following HTML. 我有以下HTML。

<div class="daddy">
  <div class="kid1">
    <span>$10</span>
  </div>
  <div class="kid2">
    <span>Nothing to see here</span>
  </div>
  <div class="kid3">
    <span>$20</span>
  </div>    
</div>

Thanks 谢谢

You can iterate over each span element and check if it's innerHTML contains $ symbol. 您可以遍历每个span元素,并检查它的innerHTML包含$符号。 If so, return all these span s and their position ( index ). 如果是这样,则返回所有这些span s及其位置( index )。

Once you have their index position, you can modify them as you like. 一旦获得它们的index位置,就可以根据需要对其进行修改。

Note : No jQuery needed in this solution. 注意 :此解决方案不需要jQuery

Example : You can push every index of each matched span to an array. 示例 :您可以将每个匹配span每个index推入数组。 In this particular case, it would look like var matchedIndexes = [0, 2] . 在这种特定情况下,它看起来像var matchedIndexes = [0, 2] Then basically, if you want to change eg the first element - simply use matchedIndexes[0] . 然后基本上,如果您想更改例如第一个元素-只需使用matchedIndexes[0]

 var elems = document.getElementsByTagName('span'), matchedIndexes = []; Array.from(elems).forEach(function(v,i){ if (/\\$/.test(v.innerHTML)){ matchedIndexes.push(i); } }); //change html of the first element elems[matchedIndexes[0]].innerHTML = '$15'; //or change the last matched element elems[matchedIndexes[matchedIndexes.length-1]].innerHTML = '$99'; 
 <div class="daddy"> <div class="kid1"> <span>$10</span> </div> <div class="kid2"> <span>Nothing to see here</span> </div> <div class="kid3"> <span>$20</span> </div> </div> 

why not having an array with values to be updated and query the span which contains '$' and update accordingly? 为什么不具有要更新值的数组并查询包含'$'并进行相应更新?

 var ratesToUpdate = [15, 40, 70]; var spans = $("form[action='/cart'] span:contains('$')"); spans.each(function(index, span){ $(span).text('$'+ratesToUpdate[index]); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <form action="/cart"> <div class="daddy"> <div class="kid1"> <span>$10</span> </div> <div class="kid2"> <span>Nothing to see here</span> </div> <div class="kid3"> <span>$20</span> </div> <div class="kid4"> <span>nothing</span> </div> <div class="kid5"> <span>nothing</span> </div> <div class="kid5"> <span>$50</span> </div> </div> </form> 

Would this solve your problem? 这样可以解决您的问题吗?

var values = [15, 40];
$("form[action='/cart'] span:contains('$')").each(
    function(index, element)
    {
        element.value = '$'+ values[index];
    });

I like the answer Kind user gave - but here is my stab at it. 我喜欢Kind user给出的答案-但这是我的目标。 I couldn't make it more dynamic as the addition values are not similar (first one is added by 5, third is doubled...) 由于加法值不相似,我无法使其更具动态性(第一个加法5,第三个加倍...)

JSFiddle Here: https://jsfiddle.net/rL36jv8m/ JSFiddle此处: https ://jsfiddle.net/rL36jv8m/

Your HTML 您的HTML

<div class="daddy">
  <div class="kid1">
    <span>$10</span>
  </div>
  <div class="kid2">
    <span>Nothing to see here</span>
  </div>
  <div class="kid3">
    <span>$20</span>
  </div>    
</div>

Array of addition values to corresponding divs. 对应div的附加值数组。

var addValues = [
    5,
  '',
  20
]

Iteration: 迭代:

$('.daddy').children().each(function(i, o) {
    var elem = $(o).find('span');
    if (elem.html().indexOf('$') === 0) {
    elem.html('$' + (parseInt(elem.html().replace('$', '')) + addValues[i]))
  }
})

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM