我试图用android做一些事情,在webview中编辑html,向页面发送js命令,我有这个代码工作
function changeColor() {
var selection;
//Get the selected stuff
if(window.getSelection)
selection = window.getSelection();
else if(typeof document.selection!="undefined")
selection = document.selection;
//Get a the selected content, in a range object
var range = selection.getRangeAt(0);
//If the range spans some text, and inside a tag, set its css class.
if(range && !selection.isCollapsed){
if(selection.anchorNode.parentNode == selection.focusNode.parentNode)
alert(selection.anchorNode){
var span = document.createElement('span');
span.style.color = 'red';
range.surroundContents(span);
}
}
}
疑点是:
1) 如果已有其他样式,如何添加多种样式?例如;背景,粗体和斜体,不必同时出现
2)如何获取所选段落的html,html是这样的:
<p class="paragrafo" style="text-indent: 40px" align="justify" id="p4"> **<span style="color:black">Text Hereeee.</span>
<div id="c4" class="comment"></div>**</p>
我需要上面
中的 html 代码,以便稍后恢复它最佳答案
部分解决:
1号的解决方案:
我不知道=(
2号的解决方案:
function highlightSelection() {
var selection;
//Get the selected stuff
if(window.getSelection)
selection = window.getSelection();
else if(typeof document.selection!="undefined")
selection = document.selection;
//Get a the selected content, in a range object
var range = selection.getRangeAt(0);
//If the range spans some text, and inside a tag, set its css class.
if(range && !selection.isCollapsed)
{
if(selection.anchorNode.parentNode == selection.focusNode.parentNode)
{
var span = document.createElement('span');
span.style.color = 'red';
range.surroundContents(span);
var x = window.getSelection()
var z = x.anchorNode.parentNode
//o que vai ta dentro do p em html
var html = z.parentNode.innerHTML; //here the content
var id = z.parentNode.id; //here show the id of the element
}
}
}
关于javascript - 如何返回选定的文本父元素html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31109319/