我正在使用window.getSelection()来获取所选文本.但是,如果我也选择了一个图像,它也返回图像的alt.
例:
<img src="someSrc.jpg" alt="image_alt" /> My text here ...
Run Code Online (Sandbox Code Playgroud)
如果我也选择了一个图像,它会返回
image_alt这里的文字......
但我只需要
我的文字在这里......
有没有办法只获得文字,没有alt?
非常感谢
我有点困惑为什么这段代码不起作用!
HTML标记:
<div id="diva"><b>Score</b> some <i>goals</i></div>
<div id="soda"></div>
Run Code Online (Sandbox Code Playgroud)
JavaScript代码:
function GetSelectedText () {
if (window.getSelection) { // all browsers, except IE before version 9
var range = window.getSelection ();
alert (range.toString ());
}
else {
if (document.selection.createRange) { // Internet Explorer
var range = document.selection.createRange ();
alert (range.text);
}
}
}
var butn = document.getElementById("soda");
butn.onclick = function(){
GetSelectedText();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用handsontable创建一些类似excel的电子表格,我需要检索用户选择的数据以使用gRaphael创建图表.但是当我尝试使用此代码甚至警告数据选择参数时:
var ht = $('#dataTable0').data('handsontable');
var sel = ht.getSelected();
alert(sel[0]);
Run Code Online (Sandbox Code Playgroud)
我在警报窗口中写了"未定义".有人能告诉我如何修复此代码吗?
我想在 iframe 设计模式中获取选定的内容。我正在使用以下代码。
function getIframeSelectionText(iframe) {
var win = iframe.contentWindow;
var doc = iframe.contentDocument || win.document;
if (win.getSelection) {
return win.getSelection();
} else if (doc.selection && doc.selection.createRange) {
return doc.selection.createRange().text;
}
}
Run Code Online (Sandbox Code Playgroud)
我能够获取文本,因为我无法获取也被选中的图像。请帮忙。
这是我的页面来源:
<body>
<div>
<p><span id="1">word</span> <span id="2">word</span> </p>
<div><span id="3">word</span> <span id="4">word</span></div>
<ul>
<li><span id="5">word</span> <span id="6">word</span></li>
</ul>
</ul>
<p><span id="7">word</span> <span id="8">word</span> <strong><span id="9">word</span></strong> </p>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我想突出显示(应用新类)用户选择的跨度并获取它的 ID。
我可以通过 window.getSelection() 获取用户选择的内容。但我不知道如何获取选择的文本节点。
提前致谢,洛根
我的问题正是如此,但在上下文中我想检查选择对象,比较anchorNode和focusNode,如果它们不同,则找到第一个公共父元素.
var selected = window.getSelection();
var anchor = selection.anchorNode;
var focus = selection.focusNode;
if ( anchor != focus ) {
// find common parent...
}
Run Code Online (Sandbox Code Playgroud) 在我的程序的一部分,我有一个JList关于位置的列表,我有一个API,它应该使用一个项目,JList并打印出该位置的天气.所以现在我做不到,因为我用
WeatherAPI chosen = locList.getSelectedIndex();
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:类型不匹配:无法从int转换为WeatherAPI.
这是有效的API示例:
LinkedList<WeatherAPI> stations = FetchForecast.findStationsNearTo("cityname");
for (WeatherAPI station : stations) {
System.out.println(station);
}
WeatherAPI firstMatch = stations.getFirst();
Run Code Online (Sandbox Code Playgroud)
所以我不想得到第一个选项,我希望得到用户选择的位置.这都是关于铸造的.我也试过这个不起作用:
WeatherAPI stations;
WeatherAPI firstMatch = stations.get(locList.getSelectedIndex());
Run Code Online (Sandbox Code Playgroud)
我得到了剩下的代码,它使用了"firstMatch",但只有当它的类型是WeatherAPI时才使用它.
我需要能够在树视图(其中有多个选择)中获取所有选定项目的更新列表。
此示例:javafx2 中的树项选择事件
显示如何一次响应/识别一个选定的项目。有没有办法一次获得所有选定的项目?类似于下面假设的非工作代码:
ArrayList<TreeItem> selectedTreeItems = new ArrayList<>();
myTreeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
selectedTreeItems.clear();//reset the list. correct?
//get a new list of children of the root
ObservableList objects = myTreeView.getRoot().getChildren();
//loop to get the selected items.
for (int i = 0; i < objects.size(); i++) {
TreeItem object = (TreeItem) objects.get(i);
if (thisObjectIsSelected(object)) {
selectedTreeItems.add(object);
}
}
}
});
privatevoid thisObjectIsSelected(TreeItem item){
//what do I do here?
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何实现我想要的。任何帮助是极大的赞赏!
我想突出显示使用Javascript更改所选文本颜色的功能.我使用以下方法.
function android_selection_highlight(replacrmenthtml){
try {
if (window.getSelection) {
sel = window.getSelection();
var range = sel.getRangeAt(0);
var selectionStart = $("<span style=\"color:red\">");
var startRange = document.createRange();
startRange.setStart(range.startContainer, range.startOffset);
var selectionEnd = $("</span>");
var endRange = document.createRange();
endRange.setStart(range.endContainer, range.endOffset);
startRange.insertNode(selectionStart[0]);
endRange.insertNode(selectionEnd[0]);
}
}
catch (e) {
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我调用方法时它会给出DOM异常.我认为当我在所选文本前面插入起始span标记时,它会破坏DOM结构,因为那时没有结束标记.如何解决这个问题呢?
编辑:会有一个高亮显示按钮.选择文本,如果用户单击突出显示按钮,则所选文本的文本颜色将更改.