尝试以编程方式选择 UIWebView 中的文本时,setStart 和 setEnd 抛出错误

setStart and setEnd throwing error when trying to programmatically select text in UIWebView(尝试以编程方式选择 UIWebView 中的文本时,setStart 和 setEnd 抛出错误)
本文介绍了尝试以编程方式选择 UIWebView 中的文本时,setStart 和 setEnd 抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 UIWebView 中使用的一些 HTML:

点击链接后,我想以编程方式选择 UIWebView 中的链接文本(因此这是一个链接"现在将使用蓝色的 iOS 文本选择工具突出显示).这就是我在 a 标签的 href 中发送一些范围信息的原因.这是我用来尝试进行编程选择的调用:

//设置内容可编辑 true[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"selectElementContentsInRange(document.getElementById('1'), 10, 20)"]];

这是javascript函数:

function selectElementContentsInRange(el, first, second) {var range = document.createRange();alert("在这里做");range.setStart(el.firstChild, first);alert("这里不行");range.setEnd(el.firstChild, second);var sel = window.getSelection();sel.removeAllRanges();sel.addRange(范围);}

正如您在警报中看到的那样,它不会通过 range.setStart() 调用而不会引发错误.Safari javascript 调试器给了我这个错误:

IndexSizeError:DOM 异常 1:索引或大小为负数,或大于允许值.

我尝试了很多不同的方法,但没有任何效果.我完全不知所措,非常感谢您对如何做到这一点的一些见解.我开始怀疑是否有可能以编程方式在 UIWebView 中选择文本.我错了吗?有人做过吗?

解决方案

问题是你试图在 <div> 元素的第一个子节点中设置范围的边界,如果您的 HTML 确实看起来像问题中的那样,它要么是一个纯空格文本节点,要么是 <span> 元素,如果你的实际 HTML 中没有空格.在后一种情况下,即在元素中设置范围边界,偏移量表示边界之前元素的子节点数.

Here is some HTML that I am working with in my UIWebView:

<div id = "1">
    <span style = "background-color:red">
         <a href = "10&20">
              This is a link
         </a>
    </span>
</div>

Once the link is tapped, I want to programmatically select the link text in the UIWebView (so "This is a link" would now be highlighted with the blue iOS text selection tool). This is why I am sending some range information in the href of the a tag. This is the call I am using to try and make this programmatic selection:

//set content editable true
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"selectElementContentsInRange(document.getElementById('1'), 10, 20)"]];

And this is the javascript function:

function selectElementContentsInRange(el, first, second) {
    var range = document.createRange();
    alert("Makes it here");
    range.setStart(el.firstChild, first);
    alert("Doesn't make it here");
    range.setEnd(el.firstChild, second);

    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}

And as you can see with the alerts, it doesn't make it past the range.setStart() call without throwing an error. The Safari javascript debugger is giving me this error:

IndexSizeError: DOM Exception 1: Index or size was negative, or greater than the allowed value.

I have tried so many different things, and nothing is working. I am completely at my wits end and would really appreciate some insight into how to do this. I am beginning to doubt that it is even possible to programmatically select text in UIWebView. Am I wrong? Has anyone done this?

解决方案

The problem is that you're trying to set the range's boundaries in the first child node of the <div> element, which is either a white space-only text node if your HTML really does look like it does in the question, or the <span> element if there is no white space in your actual HTML. In the latter case, i.e. setting a range boundary in an element, the offset represents the number of child nodes of the element preceding the boundary.

这篇关于尝试以编程方式选择 UIWebView 中的文本时,setStart 和 setEnd 抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)