繁体   English   中英

与父元素相比,如何更改跨度文本

[英]How to change span text than in the parent element

我正在使用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不是包含跨度为spanDefCat li ,就像您在代码中看到的那样,然后将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>

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

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

将此代码添加到.on函数中

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

这是执行您想要的代码:

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

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

暂无
暂无

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

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