Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
Jquery 替换元素中特定字符后的字符串中的文本?_Jquery - Fatal编程技术网

Jquery 替换元素中特定字符后的字符串中的文本?

Jquery 替换元素中特定字符后的字符串中的文本?,jquery,Jquery,JQuery非常新 我试图在元素中去掉出现在特定字符后面的字符串中的文本 我明白了: Lorem ipsum door sit amet:concertetur adipising 我需要这个: <h3>Lorem ipsum dolor sit amet</h3> Lorem ipsum dolor sit amet 我是个新手,非常感谢你提供的任何帮助。 谢谢 最简单的方法 $('h3').text(function(i, text) { return te

JQuery非常新

我试图在元素中去掉出现在特定字符后面的字符串中的文本

我明白了:

Lorem ipsum door sit amet:concertetur adipising

我需要这个:

<h3>Lorem ipsum dolor sit amet</h3>
Lorem ipsum dolor sit amet
我是个新手,非常感谢你提供的任何帮助。 谢谢

最简单的方法

$('h3').text(function(i, text) {
   return text.split(':')[0];
});

…但如果存在子元素,则不包括您

此代码将

var searchText = function(parentNode, regex, callback) {

    var childNodes = parentNode.childNodes,
        node;

    for (var i = 0, length = childNodes.length; i < length; i++) {

        node = childNodes[i];

        if (node.nodeType == 0) {

            var tag = node.tagName.toLowerCase();

            if (tag == 'script' || tag == 'style') {
                continue;
            }

            searchText(node);

        } else if (node.nodeType == 3) {

            while (true) {
                // Does this node have a match? If not, break and return.
                if (!regex.test(node.data)) {
                    break;
                }

                node.data.replace(regex, function(match) {

                    var args = Array.prototype.slice.call(arguments),
                        offset = args[args.length - 2],
                        newTextNode = node.splitText(offset);

                    callback.apply(window, [node].concat(args));
                    newTextNode.data = newTextNode.data.substr(match.length);
                    node = newTextNode;

                });
            }
        }
    }
}

searchText($('h3')[0], /:.*$/, function(node) {
    $(node).next().remove();
});
var searchText=function(parentNode、regex、回调){
var childNodes=parentNode.childNodes,
节点;
对于(var i=0,length=childNodes.length;i


我根据一些不使用jQuery库的代码改编了这段代码。使用jQuery可以使它稍微优雅一些(例如
children()
each()
makeArray()
,等等)

var x="abcd:efgh";

var mysplit=x.split(":");

var firsthalf=mysplit[0]; // = abcd
var otherhalf=mysplit[1];  // = efgh
//遍历每个``标记
$('h3')。每个(函数(索引、值){
//缓存当前``元素并获取其文本
变量$this=$(值),
text=$this.text();
//检查是否存在冒号
如果(text.indexOf(':')>0){
//在冒号处拆分文本
text=text.split(“:”);
//将当前``元素的文本设置为第一个冒号之前的文本
$this.text(text[0]);
}
});
//iterate through each `<h3>` tag
$('h3').each(function (index, value) {

    //cache the current `<h3>` element and get its text
    var $this = $(value),
        text  = $this.text();

    //check for the existence of a colon
    if (text.indexOf(':') > 0) {

        //split the text at the colon
        text = text.split(':');

        //set the text of the current `<h3>` element to the text before the first colon
        $this.text(text[0]);
    }
});