Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-替换span父文本是否正在删除span元素?_Javascript_Php_Jquery_Html_Smarty - Fatal编程技术网

Javascript jQuery-替换span父文本是否正在删除span元素?

Javascript jQuery-替换span父文本是否正在删除span元素?,javascript,php,jquery,html,smarty,Javascript,Php,Jquery,Html,Smarty,我有一些代码替换了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?dispatc

我有一些代码替换了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}
{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)}
$(文档).ready(函数(){
$('{$customer.user_id}{u 1').parent().text('{$ac_firstname}{$ac_lastname}');
});
{/if}
解决方案是:

<script type="text/javascript">

    $(document).ready(function(){

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

    });

</script>

$(文档).ready(函数(){
$('#off_user{$customer.user_id}{u 1')。parent().contents().last()[0]。textContent=“{$ac_firstname}{$ac_lastname}”;
});

删除
.parent()
或添加
.find(jquery\u selector\u for\u span)
之前。text()
将要在span中替换的文本包装为该span的类和目标。我无法将该模板修改为核心模板,因为它将在软件更新时被替换,因此尝试此JS hack/Smarty hack解决方案。若我删除父项不起作用,我唯一可以附加JS的是span ID,所以需要选择它,只替换其中的文本,而不删除其他元素。我用预期的内容更新了问题,以便更好地了解我的意思。只获取文本节点的引用并更新它-各种现有的堆栈溢出问题可以帮助解决这一问题,我已将此问题作为我找到的第一个问题的副本关闭。或者首先拆离跨距,然后更新文本,然后将跨距重新插入。