【发布时间】:2011-10-05 02:16:46
【问题描述】:
可能重复:
How to use the Javascript to add/remove the CSS/colour style to the html page?
我有一个关于 HTML 和 javascript 的问题。我得到了以下段落。
function add_span(){
// ??
}
<input type="button" onclick="add_span()" value="add span"/>
<p> statement1, statement2, statement3 </p>
用户之后有没有可能出现如下结果
- 用鼠标选择突出显示的文本
- 点击按钮
例如
- 突出显示“语句 1”
- 点击按钮
预期结果:
<input tupe="button" onclick="add_span()" value"add span"/>
<p> <span class="ABC">statement1,</span> statement2, statement3 </p>
##### 更新了代码,但没有工作
// updated code in the add_span
var selectedText;
if (window.getSelection)
{
selectedText = window.getSelection();
}
else if (document.getSelection) // FireFox
{
selectedText = document.getSelection();
}
else if (document.selection) // IE 6/7
{
selectedText = document.selection.createRange().text;
}
//create the DOM object
var newSpan = document.createElement('span');
// add the class to the 'spam'
newSpan.setAttribute('class', 'ABC');
document.getElementById('text').appendChild(newSpan);
var selectedTextNode = document.createTextNode();
newSpan.appendChild(selectedTextNode);
【问题讨论】:
-
我希望它们只是写问题时的拼写错误,但您拼错了
type,并且在您的value元素上的value之后也缺少=。 -
你already asked这个问题。
标签: javascript html