Javascript jQuery:如何用文本区域的$(';#myId';).attr(';value';)中的另一个文本替换某些文本?

Javascript jQuery:如何用文本区域的$(';#myId';).attr(';value';)中的另一个文本替换某些文本?,javascript,jquery,html,Javascript,Jquery,Html,如何用文本区域的$('#myId').attr('value')中的另一个文本替换某些文本 我试过这样的方法: $(function() { var selection; var replaceStr; var prependStr = ""; var appendStr = ""; var temp = ""; $('input[type="button"]').click(function() { if($(this).a

如何用文本区域的
$('#myId').attr('value')
中的另一个文本替换某些文本

我试过这样的方法:

$(function() {
    var selection;
    var replaceStr;

    var prependStr = "";
    var appendStr = "";

    var temp = "";

    $('input[type="button"]').click(function() {
        if($(this).attr('id') == 'underline')
        {
            prependStr = "<span style = 'text-decoration: underline;'>";
            appendStr = "</span>";
        }
        else if($(this).attr('id') == 'italic')
        {
            prependStr = "<span style = 'font-style: italic;'>";
            appendStr = "</span>";
        }
        else if($(this).attr('id') == 'bold')
        {
            prependStr = "<span style = 'font-weight: bold;'>";
            appendStr = "</span>";
        }
        else if($(this).attr('id') == 'center')
        {
            prependStr = "<span style = 'text-align: center;'>";
            appendStr = "</span>";
        }
        else if($(this).attr('id') == 'link')
        {
            prependStr = "<a href = ''>";
            appendStr = "</a>";
        }
        else if($(this).attr('id') == 'img')
        {
            prependStr = "<img src = '";
            appendStr = "' />";
        }
        else if($(this).attr('id') == 'h1')
        {
            prependStr = "<h1>";
            appendStr = "</h1>";
        }
        else if($(this).attr('id') == 'h2')
        {
            prependStr = "<h2>";
            appendStr = "</h2>";
        }
        else if($(this).attr('id') == 'h3')
        {
            prependStr = "<h3>";
            appendStr = "</h3>";
        }

        $('#page').focus();
        selection = window.getSelection();

        if(selection != '')
        {
            replaceStr = prependStr+selection+appendStr;

            temp = $('#page').attr('value');
            temp.replace(selection, replaceStr);//Not working

            $('#page').attr('value', temp);
        }
    });

});
$(函数(){
var选择;
var-replaceStr;
var prependStr=“”;
var appendStr=“”;
var temp=“”;
$('input[type=“button”]”)。单击(函数(){
if($(this.attr('id')=='underline')
{
prependStr=“”;
appendStr=“”;
}
else if($(this.attr('id')=='italic'))
{
prependStr=“”;
appendStr=“”;
}
else if($(this).attr('id')=='bold')
{
prependStr=“”;
appendStr=“”;
}
else if($(this.attr('id')=='center'))
{
prependStr=“”;
appendStr=“”;
}
else if($(this.attr('id')='link'))
{
prependStr=“”;
}
else if($(this.attr('id')=='img'))
{
prependStr=“”;
}
else if($(this.attr('id')='h1'))
{
prependStr=“”;
appendStr=“”;
}
else if($(this).attr('id')=='h2')
{
prependStr=“”;
appendStr=“”;
}
else if($(this).attr('id')=='h3')
{
prependStr=“”;
appendStr=“”;
}
$('第页')。焦点();
selection=window.getSelection();
如果(选择!='')
{
replaceStr=prependStr+selection+appendStr;
temp=$('#page').attr('value');
temp.replace(selection,replaceStr);//不工作
$('#page').attr('value',temp);
}
});
});
除了函数.replace(),整个代码都正常工作。我在谷歌上搜索了很多东西,但是我没有在$(“#page”).attr('value')上找到一个函数来实现这一点。

看看

temp.replace(selection,replaceStr)不会更改变量“temp”。它返回一个新字符串

这样做: 温度=温度替换(选择,替换STR); $('#page').attr('value',temp)

看一看

temp.replace(selection,replaceStr)不会更改变量“temp”。它返回一个新字符串

这样做: 温度=温度替换(选择,替换STR); $('#page').attr('value',temp)

$('#page').attr('value')
返回一个字符串,并且字符串是不可变的,这意味着当您执行
临时替换(selection,replaceStr)时它实际上并没有改变string对象,它所做的只是创建另一个string对象,所以它不会改变textarea的值

你可以得到你想要的

var textarea = $('#page');
textarea.val(textarea.val().replace(selection, replaceStr));
顺便说一下,
jquerylement.val(value)
相当于
jquerylement.attr('value',value)
,它用于设置值。

$('#page').attr('value')
返回一个字符串,并且字符串是不可变的,这意味着当您执行
临时替换(selection,replaceStr)时它实际上并没有改变string对象,它所做的只是创建另一个string对象,所以它不会改变textarea的值

你可以得到你想要的

var textarea = $('#page');
textarea.val(textarea.val().replace(selection, replaceStr));

顺便说一下,
jqueryElement.val(value)
相当于
jqueryElement.attr('value',value)
用于设置值。

像这里这样使用val()和replace()函数?谢谢,它正在工作。var textarea=$(“#页”);val(textarea.val().replace(selection,replaceStr));像这样使用val()和replace()函数?谢谢,它正在工作。var textarea=$(“#页”);val(textarea.val().replace(selection,replaceStr));谢谢大家,我根本不明白replace()是如何工作的!谢谢大家,我根本不明白replace()是如何工作的!我在谷歌搜索时看到了这个链接,但不明白,谢谢。我在谷歌搜索时看到了这个链接,但不明白,谢谢。