jQuery - 替换 span 父文本就是删除 span 元素?
jQuery - replacing spans parent text is removing the span elements?
我有一些代码替换了 TD 中的文本,但它也删除了 TD 中的其他元素,例如 span,有什么方法可以替换文本吗?
替换前
<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&user_id=81257937&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&user_id=81257937&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>
聪明 / 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}
解决方案是:
<script type="text/javascript">
$(document).ready(function(){
$('#off_user_{$customer.user_id}_1').parent().contents().last()[0].textContent="{$ac_firstname} {$ac_lastname}";
});
</script>
我有一些代码替换了 TD 中的文本,但它也删除了 TD 中的其他元素,例如 span,有什么方法可以替换文本吗?
替换前
<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&user_id=81257937&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&user_id=81257937&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>
聪明 / 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}
解决方案是:
<script type="text/javascript">
$(document).ready(function(){
$('#off_user_{$customer.user_id}_1').parent().contents().last()[0].textContent="{$ac_firstname} {$ac_lastname}";
});
</script>