我在我的项目中使用window.getSelection()方法来生成可引用的文本
它在所有现代浏览器中运行得很好,不管IE10如何.在IE10控制台中返回正确的文本,但选择中断.
我使用的唯一代码:
text = window.getSelection().toString();
console.log(text);
Run Code Online (Sandbox Code Playgroud)
此代码调用mouseup事件.
有谁知道解决方案?
javascript internet-explorer getselection internet-explorer-10
我试图捕获结果,Intent.createChooser以了解用户选择共享的应用程序.
我知道有很多与此相关的帖子:
但是这些帖子有些陈旧,我希望可能会有一些新的发展.
我正在尝试实现共享操作,而不会出现在菜单中.我想要的最接近的解决方案由ClickClickClack提供,他建议实施自定义应用程序选择器,但这似乎很重要.此外,似乎可能会有一些Android钩子来获取所选的应用程序,例如ActivityChooserModel.OnChooseActivityListener.
我的MainActivity中有以下代码,但该onShareTargetSelected方法永远不会被调用.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage());
sendIntent.setType("text/plain");
Intent intent = Intent.createChooser(sendIntent, getResources().getText(R.string.share_prompt));
ShareActionProvider sap = new ShareActionProvider(this);
sap.setShareIntent(sendIntent);
sap.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
System.out.println("Success!!");
return false;
}
});
startActivityForResult(intent, 1);
Run Code Online (Sandbox Code Playgroud) 我想以编程方式选择页面上的所有文本,其结果与我按下组合键Ctrl + A的结果完全相同.
使用的问题document.getSelection().selectAllChildren(body)是选择还将包括用户无法选择的文本节点,即CSS中定义的<script> </script>节点user-select:none:
<div style="-moz-user-select:none">将被选中</div>
modify选择对象的方法可以像这样使用:
selection.modify("extend", "forward", "documentboundary");
将选择从文档的开头扩展到结尾,这将忽略任何脚本或样式元素的内容和元素-moz-user-select:none- 遗憾的是Firefox不允许documentboundary作为3.参数和word无济于事.
有没有快速的方法来实现这一目标?只需要在Firefox中工作.
编辑(不太好解决方案):选择第一个文本节点,然后selection.modify('extend', 'forward', 'line')重复使用,selection.focusNode而不等于最后一个文本节点 - 但根据文档的长度,这需要几秒钟!
编辑: selection.selectAllChildren将在Chrome中按预期工作,其中user-select:none不会选择文本元素- 不幸的是,在FF中有不同的行为.
编辑:这不是这篇文章的重复,因为我既不解决contenteditable元素也不关心它们;)
我暂时忘记了跨浏览器的兼容性,我只想让它工作.我正在做的是尝试修改位于typegreek.com的脚本(你可能不需要知道这个).基本脚本可以在这里找到.基本上它的作用是当你输入字符时,它会将你输入的字符转换成希腊字符并将其打印到屏幕上.我想要做的是让它在contentEditable div上工作(它只适用于Textareas)
我的问题是这个函数:用户键入一个键,它被转换为一个希腊键,然后转到一个函数,它通过一些if进行排序,最后它在哪里可以添加div支持.这是我到目前为止所拥有的,
myField是div,myValue是希腊字符.
//Get selection object...
var userSelection
if (window.getSelection) {userSelection = window.getSelection();}
else if (document.selection) {userSelection = document.selection.createRange();}
//Now get the cursor position information...
var startPos = userSelection.anchorOffset;
var endPos = userSelection.focusOffset;
var cursorPos = endPos;
//Needed later when reinserting the cursor...
var rangeObj = userSelection.getRangeAt(0)
var container = rangeObj.startContainer
//Now take the content from pos 0 -> cursor, add in myValue, then insert everything after myValue to the end of the line.
myField.textContent …Run Code Online (Sandbox Code Playgroud) 如何使用Javascript在页面上获取所选内容的原始HTML?为了简单起见,我坚持使用浏览器支持window.getSelection.
这是一个例子; 两者之间的内容|代表我的选择.
<p>
The <em>quick brown f|ox</em> jumps over the lazy <strong>d|og</strong>.
</p>
Run Code Online (Sandbox Code Playgroud)
我可以使用以下Javascript捕获并警告规范化的HTML.
var selectionRange = window.getSelection().getRangeAt(0);
selectionContents = selectionRange.cloneContents(),
fragmentContainer = document.createElement('div');
fragmentContainer.appendChild(selectionContents);
alert(fragmentContainer.innerHTML);
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,警报内容将折叠尾随元素并返回字符串<em>ox</em> jumps over the lazy <strong>d</strong>.
我该如何归还字符串ox</em> jumps over the lazy <strong>d?
我在iframe中有一个带有designMode的富文本编辑器,可以对小块文本进行语法高亮显示.我希望它更新keyup上的突出显示,但是搞乱DOM会导致帧模糊,因此每次按下一个键时,插入符号都会消失,你不能再输入了.如果解析器可以记住插入符的位置,然后重新聚焦iframe并替换插入符号,那就不会有问题.我已经阅读了getSelection()它的亲戚,但显然onkeyup删除了选择,至少在Chrome中 - getSelection()在onkeyup内调用总是产生一个空选择.有没有解决的办法?
这就是我所拥有的:
<iframe>
<html>
<head>
<script>
function parse() {
if(window.getSelection().type != 'None') {
var range = window.getSelection().getRangeAt(0);
}
var text = document.body.textContent;
//Parse text, output is stored in newtext
document.body.innerHTML = newtext;
document.body.focus();
if(range) {
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
</script>
</head>
<body onload="document.designMode = 'on'; document.onkeyup = parse;">
Text entered appears here
</body>
</html>
</iframe>
Run Code Online (Sandbox Code Playgroud) 我正在测试该getSelection()方法,我希望我的程序获取特定文本段落中的选定文本并将其显示在 div 标签中。我使用了以下代码:
var txt = document.getSelection();
document.getElementById("display").innerHTML = "The text you have selected is: " + txt + ".";
Run Code Online (Sandbox Code Playgroud)
但是,我希望程序仅获取段落本身中所做的选择,而不是整个文档中的选择。我尝试使用document.getElementById("id").getSelection();但没有成功。
我怎样才能使它getSelection()只适用于某个元素?
是否有办法在 window.getSelection() 之后获取下一个字符?我需要检查所选文本后面的字符是否是空格...
编辑:谢谢您的回答!我基本上使用此链接 来突出显示文本,但希望将内容限制为完整的单词。我使用下面提出的解决方案(由史蒂文)作为起点;我认为以下应该有效:
sel = window.getSelection();
var text = sel.anchorNode.nodeValue;
var index = sel.baseOffset + sel.focusOffset-1;
var isSpace = text[index] === undefined;
if (isSpace) {
alert("space");
}
Run Code Online (Sandbox Code Playgroud)
(在上面的链接中,我在 makeEditableAndHighlight 函数调用之后立即使用了此代码)。
我试图在用户点击特定栏时触发使用Google的Chart API创建新的BarChart.我想我理解这些概念,并希望至少让getSelection()函数工作并显示用户点击的栏.但每次,当你点击栏时,它只是冻结了显示屏而没有java警报.有什么想法吗?
这是代码:
<script type="text/javascript">
google.setOnLoadCallback(drawChart);
function drawChart() {
var visualization = new google.visualization.BarChart(document.getElementById('acctmeta'));
var json_data = new google.visualization.DataTable({{acctmeta_json}});
visualization.draw(json_data, {width: 850, height: 600, title: 'Collection Level Populated Metadata Fields',
chartArea: {width:"50%"},
vAxis: {title: 'Collection Title/ID', titleTextStyle: {color: 'green'}},
hAxis: {logScale:false, title:'Fields Populated', titleTextStyle: {color: 'green'}}
});
// Add our selection handler.
google.visualization.events.addListener(visualization, 'select', selectHandler);
// The selection handler.
// Loop through all items in the selection and concatenate
// a single message from all of them.
function selectHandler() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用jquery mobile指定的touchend(或taphold事件,两者都不会触发).我正在使用phonegap平台为Android开发应用程序.我的AVD运行Android 4.0.3,我的phonegap版本是1.3.0.
正如我正在使用phonegap,我在javascript中编码,目的是使用以下代码在对单词进行长按之后恢复用户选择的文本.
var selectAction = function(){
LOGGER.log('event.js : selectAction');
};
function selectMobile(component){
component.addEventListener('touchend',selectAction,false);
}
Run Code Online (Sandbox Code Playgroud)
它实际上工作正常,如果我点击相对较快,事件将启动,我可以得到日志消息.但是,快速触摸不会选择文本(我有更多的代码采取选定的文本,但这不是问题)
当用户长时间按下Android时,会弹出一个默认的"操作菜单",这似乎阻止了更多事件的启动.因此,当选择实际文本时,我从未进入我的selectAction.
有人知道如何在Android平台上的html/javascript中正确获取所选文本吗?
非常感谢你.
getselection ×10
javascript ×8
selection ×4
android ×2
html ×2
bar-chart ×1
cordova ×1
java ×1
range ×1
string ×1