jQuery-替换spans父文本是否要删除span元素? [重复]

问题描述 投票:-1回答:1

我有一些代码替换TD中的文本,但它也删除了TD中的其他元素,例如跨度,是否有任何方法可以替换文本?

更换前

<td>
    <span alt="Expand the sublist of items" title="Expand the sublist of items" id="on_user_81257937_1" class="cm-combination-carts" onclick="Tygh.$.ceAjax('request', 'admin.php?dispatch=cart.cart_list&amp;user_id=81257937&amp;c_company_id=1', {result_ids: 'cart_products_81257937_1,wishlist_products_81257937_1', caching: true});"><span class="exicon-expand"></span></span>
    <span alt="Collapse the sublist of items" title="Collapse the sublist of items" id="off_user_81257937_1" class="hidden cm-combination-carts"><span class="exicon-collapse"></span></span>

    Unregistered customer        
</td>

用我的JS替换后

<td>James Murphy</td>

期待什么

<td>
    <span alt="Expand the sublist of items" title="Expand the sublist of items" id="on_user_81257937_1" class="cm-combination-carts" onclick="Tygh.$.ceAjax('request', 'http://beanbags.ambientlounge.com/admin.php?dispatch=cart.cart_list&amp;user_id=81257937&amp;c_company_id=1', {result_ids: 'cart_products_81257937_1,wishlist_products_81257937_1', caching: true});"><span class="exicon-expand"></span></span>
    <span alt="Collapse the sublist of items" title="Collapse the sublist of items" id="off_user_81257937_1" class="hidden cm-combination-carts"><span class="exicon-collapse"></span></span>

    James Murphy       
</td>

Smarty / JS

{assign var="ac_firstname" value=$customer.user_id|fn_get_ac_firstname}
{assign var="ac_lastname" value=$customer.user_id|fn_get_ac_lastname}

{if !empty($ac_firstname) || !empty($ac_firstname)}
    <script type="text/javascript">

        $(document).ready(function(){

            $('#off_user_{$customer.user_id}_1').parent().text('{$ac_firstname} {$ac_lastname}');

        });

    </script>
{/if}
javascript php jquery html smarty
1个回答
0
投票

解决方案是:

<script type="text/javascript">

    $(document).ready(function(){

        $('#off_user_{$customer.user_id}_1').parent().contents().last()[0].textContent="{$ac_firstname} {$ac_lastname}";

    });

</script>
© www.soinside.com 2019 - 2024. All rights reserved.