用vanilla JS替换元素中的字符串

时间:2015-07-08 07:49:10

标签: javascript jquery

我使用jQuery脚本替换元素中的文本。现在我需要使用普通的Javascript来做到这一点。

我该怎么做?

这是我目前正在使用的脚本:

nps <- data.frame(Rating=c(1,3,4,7,8,9,2,10,10));
nps$Category <- ifelse(nps$Rating<=6,'Detractor',ifelse(nps$Rating>6 & nps$Rating<9,'Passive','Promoter'));
nps;
##   Rating  Category
## 1      1 Detractor
## 2      3 Detractor
## 3      4 Detractor
## 4      7   Passive
## 5      8   Passive
## 6      9  Promoter
## 7      2 Detractor
## 8     10  Promoter
## 9     10  Promoter

3 个答案:

答案 0 :(得分:1)

使用这样的小型填充物:

function hasClass(element, cls) {
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}

您可以执行以下操作:

// Get all the elements.
var elements = document.getElementsByClassName("tp-caption");
// Iterate each.
for (var i = 0; i < elements.length; i++) {
    // Check if the text is found.
    if (elements[i].innerHTML.indexOf(".000000") != -1 && hasClass(elements[i], "black"))
        // Replace the contents.
        elements[i].innerHTML = '.1111111';
}

这将替换整个字符串。如果您只想替换该部分,请使用:

// Get all the elements.
var elements = document.getElementsByClassName("tp-caption");
// Iterate each.
for (var i = 0; i < elements.length; i++) {
    // Check if the text is found.
    if (elements[i].innerHTML.indexOf(".000000") != -1 && hasClass(elements[i], "black"))
        // Replace the contents.
        elements[i].innerHTML = elements[i].innerHTML.replace('.000000', '.1111111');
}

天哪,你甚至可以这样做:

// Get all the elements.
var elements = document.getElementsByClassName("tp-caption black");
// Iterate each.
for (var i = 0; i < elements.length; i++) {
    // Check if the text is found.
    if (elements[i].innerHTML.indexOf(".000000") > -1)
        // Replace the contents.
        elements[i].innerHTML = '.1111111';
}

// Get all the elements.
var elements = document.getElementsByClassName("tp-caption black");
// Iterate each.
for (var i = 0; i < elements.length; i++) {
    // Check if the text is found.
    if (elements[i].innerHTML.indexOf(".000000") > -1)
        // Replace the contents.
        elements[i].innerHTML = elements[i].innerHTML.replace('.000000', '.1111111');
}

对于现代浏览器,您只需使用:

// Get all the elements.
var elements = document.querySelectorAll(".tp-caption.black");
// Iterate each.
for (var i = 0; i < elements.length; i++) {
    // Check if the text is found.
    if (elements[i].innerHTML.indexOf(".000000") != -1)
        // Replace the contents.
        elements[i].innerHTML = elements[i].innerHTML.replace('.000000', '.1111111');
}

答案 1 :(得分:0)

如果你想要Vanilla JS中的所有东西,就像这样:

var arr = document.getElementsByClassName('tp-caption black');
for (var i = 0; i < arr.length; i++)
{
    var t = arr[i].innerHTML;
    t = t.replace('.000000', '.111111');

    arr[i].innerHTML = t;
}

如果你只想要Vanilla JS中的替换位,就像这样:

var t = $('.tp-caption.black').text();
$('.tp-caption.black').text(t.replace('.000000', '.111111'));

答案 2 :(得分:0)

评论为您提供 vanilla (某种)与 jQuery

匹配的内容的指示
window.addEventListener('load', function () { // $('document').ready(function () {
    Array.prototype.forEach.call(
        document.querySelectorAll('.tp-caption.black'), // $( ".tp-caption.black
        function (e) {
            if (e.textContent.indexOf('.000000') !== -1) // :contains('.000000')" )
                e.textContent = '.1111111'; // .text('.1111111');
        }
    );
}