简体   繁体   English

如何使用脚本标记中的HTML使用JavaScript替换字符串

[英]How do you do a string replace with javascript using HTML in a script tag

I am working on a plugin for WordPress and I need to add multiple settings to be able to add multiple videos. 我正在为WordPress开发插件,我需要添加多个设置才能添加多个视频。 Everything is working except for when I copy the content in the script tag, I am not able to replace a placeholder with the value I want. 除了复制script标记中的内容时,一切正常,我无法用所需的值替换占位符。

Here is what I have so far: http://jsfiddle.net/Jb6NK/1/ 这是我到目前为止所拥有的: http : //jsfiddle.net/Jb6NK/1/

(function ($) {
    var i = 0;
    $("#add-olympvm").click(function () {
        var section = $("#blank_data").text();
        section.replace(/COUNT_KEY/g, i);
        $("#olympvm_cage tbody").append(section);
        i++;
    });
}(jQuery));

What I am trying to do is replace COUNT_KEY with the value of i . 我正在尝试用i的值替换COUNT_KEY

I know this is possible but for the life of me I cannot seem to get this working. 我知道这是可能的,但是对于我一生来说,我似乎无法使它正常工作。

Any help/pointers is much appreciated. 任何帮助/指针将不胜感激。

Thanks, 谢谢,
Jeremy 杰里米

String.prototype.replace is non-destructive. String.prototype.replace是非破坏性的。 It doesn't change the original string, but creates a new one. 它不会更改原始字符串,但会创建一个新字符串。 You want this: 你要这个:

section = section.replace(/COUNT_KEY/g, i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM