文本替换javascript jquery [英] Text replace javascript jquery

查看:76
本文介绍了文本替换javascript jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,大家好,对不起我的英语。

我仍​​然会利用这个社区的专业知识和可用性。

昨天我看到了关于字符串的帖子(这个) s
为简单起见,我们删除表并尝试查看以下代码

First of all Hello to everyone, and sorry for my English.
I would still take advantage of the expertise and availability of this community.
Yesterday i see a post(this) about string sostitution.
For simplicity, we remove the tables and try to look at the following code

<div id="find">
    <div>(this match)</div>
    <div>(this match)</div>
    <div>some text1
        <div></div>
        <div>(this match)</div>
        <div>some text2</div>
        <div>some text3</div>
    </div>
</div>

在这种情况下,如果我们想要查找包含匹配一词的项目并将此替换为其他一些文字字符串,我们可以通过阅读此主题上的其他帖子找到这些代码段之一

in this case if we want to find items that contain the word "match" and replace this word with "some other text" string, we can use one of these code snippet i found by reading other posts on this topic

$(document).ready(function(){
        var elements = $('div');
        for(var i = 0; i < elements.length; i++) {
            var current = elements[i];
            if(current.children.length === 0) {
               var x=current.textContent
               if (x.indexOf('match') > 0) {current.textContent='some other text'}
            }
        } 
})

$(document).ready(function(){
    $("#find div:contains('match'):not(:has(*))").each(function () {    
         var str=$(this).text('some other text')
    })
})

$(document).ready(function(){
    $("div").each(function(){
        var str=''
        var div = $(this).clone();
        div.find("*").remove();
        str=div.text(); 
        if (str.indexOf('match') > 0) {
            $(this).text('some other text')
        }
    })
})



但是,如果以这种方式编辑html所有代码段错误


However, if you edit the html in this way all snippets are wrong

<div id="find">(this match)
    <div>(this match)
       <div>(this match)</div>
    </div>
    <div>(this match)<div>aaaa</div></div>
    <div>some text1
        <div></div>
        <div>(this match)</div>
        <div>some text2</div>
        <div>some text3</div>
    </div>
</div>

我找到了解决这个问题的方法,但我认为它不够优雅且过于冗长/ p>

I have found a solution to this problem but I think it's inelegant and overly verbose

$(document).ready(function(){
    var len =$('#find div').length
    for(i=1;i<len;i++){
        $('div:eq('+i+')').contents().addClass('passed').filter(function () {
         return $(this).text()==='(this match)' && $.trim(this.nodeValue).length
       }).replaceWith('some other text');
    }
    for(i=0;i<len;i++){
        var classx=$('div:eq('+i+')').attr('class')
        if(classx===undefined){
            var xx=$('div:eq('+i+')').contents()[0].nodeValue
            if (xx.indexOf('match') > 0) {
               $('div:eq('+i+')').contents()[0].nodeValue='some other text'
           }
        }
    }
})

请有人指导我到莫高效优雅的方式来达到同样的效果?

一如既往,提前谢谢大家,欢迎任何建议。

Please could someone direct me to a more efficient and elegant way to achieve the same result?
As always, thank you all in advance, any advice will be welcomed with pleasure.

推荐答案

我认为你想要的是这里。如果我理解你想要做什么,可以采用一种更优雅的方式来处理第一个片段:

I think what you want is here. If I understand what you want to do, a more "elegant" way of doing for the first snippet it could be:

$(document).ready(function(){
    $('#find div').each(function () {
        $(this).html($(this).html().replace('match', 'some other text'));
    });
});

至于第二个,这似乎有效(作为一个警告,它也适用于第一个片段):

As for the second, this seems to work (as a caveat, it also works on the first snippet):

function findAllText (idToStartWith) {
    $('#' + idToStartWith).html($('#' + idToStartWith).html().replace('match', 'some other text'));
    while ($('#' + idToStartWith).text().indexOf('match') > 0) {
        $('#' + idToStartWith).find('*').each(function () {
            $(this).html($(this).html().replace('match', 'some other text'));
        });
    }
}

这篇关于文本替换javascript jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆