【问题标题】:How to change span text than in the parent element如何更改跨度文本而不是父元素
【发布时间】:2013-06-28 13:37:39
【问题描述】:

我正在使用双击img

这段代码

$("#insert-zone").on("dblclick", ">div>img", function(){
 console.log($(this).parentNode.className(spanDefCat));

 var answer = confirm ("Set Defaul Cat to " +$("#txt_CategoryID").val()+"?")
    if (answer){....

.

这是 div 项

<div class="item-container cart-item sameCat">
   <div class="SetDefault">
      <img border="0" title="Carrie Style Silver Name Necklace" src="medium_101-01-071-02.jpg" alt="1102">
      <div class="item-header">
         <div class="item-body">
            <ul class="item-ul">
               <li>
                  <span class="spanDefCat">DefaultCat: 32</span>
               </li>
            </ul>
         </div>
      </div>
      <div class="item-footer"></div>
</div>

当我双击 img 时,我需要返回父级 cart-item,而不是包含带有跨度类 spanDefCatli,就像您在代码中看到的那样,并将 DefaulCat 更改为 80 或其他我需要更改跨度。

完整的 HTML

    <div class="ui-widget-content">
    <ol id="insert-zone" class="ui-droppable ui-sortable">
    <div class="item-container cart-item sameCat">
    <div class="item-container cart-item ">
    <div class="item-container cart-item sameCat">
    <div class="item-container cart-item sameCat">
   </ol>
    </div>

【问题讨论】:

  • 你可以使用$('.spanDefCat').html('80');
  • 这个&gt;div&gt;img 选择器有效吗?
  • 这个 >div>img 选择器有效吗?点击是
  • @VovaPotapov 您可以使用@User回复cmets中的某人。
  • 你可以使用$('.spanDefCat').html('80');?不,我有很多其他具有此类的项目,我只需将我的父类 .spanDefCat 更改为 80,而这一项是更改所有类

标签: javascript jquery


【解决方案1】:

这是你想要的代码:

$(function(){
    $("img").dblclick(function(){
        $(this).parent().find(".spanDefCat").each(function(){
            $(this).text("DefaultCat: 80");
        });
    });
});

这是工作小提琴:http://jsfiddle.net/ZrA8E/

【讨论】:

    【解决方案2】:

    在 .on 函数中添加这段代码

    var newCatVal = "DefaultCat: "+$("#txt_CategoryID").val();
    $(this).closest('.cart-item').find(".spanDefCat").text(newCatVal);
    

    【讨论】:

      【解决方案3】:

      你可以使用$(this).parent().find('.spanDefCat').html($("#txt_CategoryID").val())

      这将更改图像父级中所有.spanDefCat 的值。

      【讨论】:

      • 这是 100% 我一直在寻找的东西,尤其是你@Daniel Kurz
      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 2023-01-07
      • 2011-03-11
      • 2021-03-04
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多