Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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';s replace()方法_Javascript_Jquery_Replace - Fatal编程技术网

使用Javascript';s replace()方法

使用Javascript';s replace()方法,javascript,jquery,replace,Javascript,Jquery,Replace,我试图使用Javascript的内置replace()方法,但由于某些原因,它不允许我将html包装在单词周围并将其输出到文本区域 $(function() { $('#js-convert-markup').click(function() { var htmlToConvert = $('.markup_converter').val(); htmlToConvert.replace('class', '<span class="admin_st

我试图使用Javascript的内置
replace()
方法,但由于某些原因,它不允许我将html包装在单词周围并将其输出到文本区域

$(function() {
    $('#js-convert-markup').click(function() {
        var htmlToConvert = $('.markup_converter').val();
        htmlToConvert.replace('class', '<span class="admin_styles_type">class</span>');
        var convertedMarkup = $('.markup_converted').val(htmlToConvert);
    });
});​
$(函数(){
$('#js convert markup')。单击(函数(){
var htmlToConvert=$('.markup_converter').val();
替换('class','class');
var convertedMarkup=$('.markup_converted').val(htmlToConvert);
});
});​

知道原因吗?

尝试使用:

htmlToConvert = htmlToConvert.replace('class', '<span class="admin_styles_type">class</span>');
htmloconvert=htmloconvert.replace('class','class');
字符串在JavaScript中是不可变的


replace返回一个新字符串,它不会更改原始字符串。您需要执行以下操作:

htmlToConvert = htmlToConvert.replace(...)