Javascript删除了在IE8中不起作用的空格

Javascript remove spaces not working in IE8

本文关键字:不起作用 空格 IE8 删除 Javascript      更新时间:2023-09-26

我的php文件中有一个选择列表:

<select onchange="storePolishType(this.value,this.options[this.selectedIndex].text);" class="drop_down1">
<option value="10">BRP</option>           
<option value="10">Polished Ends</option>           
<option selected="selected" value="11">Sawed Ends</option>            
<option value="10">Steeled Ends</option>                    
</select> 

我在java脚本文件中有一个storePolishType函数。在该函数中,我尝试删除字符串前后的空格:

function storePolishType(pTypeID,bottom_polish_name)
{
    if(typeof bottom_polish_name != 'undefined')
    {
        bottom_polish_name = bottom_polish_name.replace(/^'s's*/, '').replace(/'s's*$/, '');
    }
} 

它在Firefox中工作正常,但在IE8中不起作用。我也尝试过$.trim() jquery的功能,它在IE8中也不起作用。

请帮我解决这个问题。

试试这个:

function trim(s){
    if(typeof(s) === 'undefined'){return;}
    return s.replace(/^'s+|'s+$/g,"");
}

如果你想使用你的函数,你可以键入" "而不是 ''s 并尝试一下。