如何通过javascript删除

时间:2016-03-05 05:01:16

标签: javascript

这是我的代码

<select name="category_id">
    <option value="0">All Categories</option>
    <option value="57">Books</option>
    <option value="63">&nbsp;&nbsp;&nbsp;Aboriginal Authors & Studies</option>
    <option value="64">&nbsp;&nbsp;&nbsnbsp;Animals & Birds</option>
    <option value="117">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Green Guide</option>
    <option value="65">&nbsp;&nbsp;&nbsp;Atlases</option>
    <option value="86">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Regional Atlases</option>
    <option value="87">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Road Atlases</option>
</select>

2 个答案:

答案 0 :(得分:8)

如果不替换格式错误的&nbsp;,您可以替换每个节点的innerHTML

var options = document.getElementsByTagName('option')
for (index = 0; index < options.length; ++index) {
  options[index].innerHTML = options[index].innerHTML.replace(/\&nbsp;/g, '');
}

工作示例:https://jsfiddle.net/2h6hqc0g/

答案 1 :(得分:2)

试试这个:

var str = "All Categories Books   Aboriginal Authors & Studies  &nbsnbsp;Animals & Birds        Green Guide   Atlases        Regional Atlases        Road Atlases";
var op = str.replace(/&nbsnbsp;/g, '');
alert(op);