Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 滚动到webview中具有给定名称属性的HTML元素_Javascript_Html_Ios_Swift - Fatal编程技术网

Javascript 滚动到webview中具有给定名称属性的HTML元素

Javascript 滚动到webview中具有给定名称属性的HTML元素,javascript,html,ios,swift,Javascript,Html,Ios,Swift,我在swift中使用UIWebView,它使用webView.loadHTMLString 下面是我正在加载的HTML示例: </p>\r\n<p> <a name="bm001">**</a> Physicists think the invisible dark matter must exist because they can see its\r\ngravitational effects on visible matter throug

我在swift中使用
UIWebView
,它使用
webView.loadHTMLString

下面是我正在加载的HTML示例:

</p>\r\n<p>
<a name="bm001">**</a>
Physicists think the invisible dark matter must exist because they can see its\r\ngravitational effects on visible matter throughout the cosmos. But no one knows \r\nwhat it's actually made of. Among the leading candidates are weakly interacting \r\nmassive particles, or WIMPs, but scientists have <span style="color:red;">hunted for them for decades with \r\nno success</span>


</p>\r\n<p><a name="bm002"></a>
Theoretically, macros could have almost any size and mass. And because dark matter \r\ndoesn't interact with regular matter, there would be nothing to stop these particles \r\nfrom zipping around unimpeded. So Starkman - along with Case Western physicist Jagjit \r\nSingh Sidhu and physicist Robert Scherrer of Vanderbilt University in Nashville \r\n- decided to do a gut check using human flesh as a dark matter detector.</p>\r\n<p>If a macro as small as a square micrometer zipped through your body at hypersonic \r\nspeed, it would deposit about as much energy in your body as a typical metal bullet, \r\nthe team calculated. But the damage it caused would be different from that of a \r\nbullet: A macro would heat the cylinder of tissue in its wake to about  10,000,000&#176; \r\nCelsius - vaporizing the tissue and leaving a path of plasma.</p>

<p><a name="bm003"></a>Next, Starkman and Sidhu plan to look for macro tracks in slabs of granite, which \r\nwould appear as cylinders of black obsidian running straight through the rock. They're \r\nstarting with a cemetery near the Case Western campus.</p>

\r\n ** 物理学家认为不可见暗物质一定存在,因为他们可以看到它对整个宇宙可见物质的引力效应。但是没有人知道\r\n它实际上是由什么制成的。在主要的候选粒子中有弱相互作用的大粒子或WIMP,但科学家几十年来一直在寻找它们,但没有成功

\r\n 理论上,宏几乎可以有任何大小和质量。因为暗物质\r\n不与普通物质相互作用,所以没有任何东西可以阻止这些粒子\r\n不受阻碍地运动。因此,斯塔克曼与Case Western物理学家贾吉特·西德胡和纳什维尔范德比尔特大学的物理学家罗伯特·舍勒决定用人体作为暗物质探测器进行肠道检查。

\r\n如果一个小到一平方微米的宏观物体以高超音速从你的身体中穿过,研究小组计算得出,它在你体内的能量大约相当于一颗典型的金属子弹。但它所造成的损害与子弹不同:一个宏会将尾迹中的圆筒状组织加热到大约10000000°\r\nCelsius-蒸发组织并留下等离子体路径

接下来,Starkman和Sidhu计划在花岗岩板中寻找宏观轨迹,这些轨迹将\r\n显示为黑色黑曜石圆柱体,笔直穿过岩石。他们正在Case Western校区附近的一个公墓里\r\n开始

现在,我想实现的功能是,按按钮滚动到姓名标签。 例如,在以下HTML内容中,有三个
标记包含不同的名称,即
name=“bm001”、name=“bm002”、name=“bm003”

现在,如果我按下按钮1,它将滚动到包含bm001的段落, 如果我按下按钮2,它将滚动到包含bm002的段落,其他标签也是如此


有什么方法可以实现此功能吗?

您可以将JavaScript注入webview以执行滚动操作,如下所示:

//在页面加载时调用此函数一次:
func setupPageScrolling()->Bool{
让函数jscode=“”
函数scrollToElement(名称){
const元素=document.querySelector(“[name=”+name+“]”);
if(元素==null){
log(“找不到名为“'+name+””的元素”;
返回false;
}
ScrollingToView({“行为”:“平滑”});
返回true;
}
scrollToElement的类型==“函数”
"""
返回webView.stringByEvaluatingJavaScript(from:functionJSCode)=“true”
}
定义要在执行滚动操作时调用的Swift函数:

func scrollTo(名称:String)->Bool{
返回webView.stringByEvaluatingJavaScript(from:“scrollToElement(““+name+”)”)”==“true”
}
根据需要打电话

参考资料:


完整工作示例(使用Xcode 11.3.1测试):
导入UIKit
让html=”“”
测试

知识本身是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德如果你不轻率,就必须为自己的行为负责。
所有人都会犯错误,但我们必须清楚地认识到这一点,因为它是一种赞美,是一种开明,是一种对发明者真实性的尊重,是一种对生命的追求,是一种理性的追求这一点也不例外,因为在劳动和就业方面,所有人都有自己的权利,也有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利,有自己的权利你是否认为自己是一个自由职业者,而不是一个自由职业者?
但我必须向你们解释,谴责快乐和赞美痛苦的错误观念是如何产生的,我将给你们一个完整的系统描述,并阐述伟大的真理探索者、人类幸福的建设者的实际教导。没有人拒绝、不喜欢或回避快乐本身,因为快乐本身就是快乐,但它是幸福因为那些不知道如何理性地追求快乐的人会遇到极度痛苦的后果。也没有人爱、追求或渴望获得痛苦本身,因为痛苦本身就是痛苦,但因为偶尔会发生一些情况,在这些情况下,辛劳和痛苦可以使他获得一些巨大的快乐我们当中有谁曾经进行过艰苦的体育锻炼,只是为了从中获得一些好处?但是,谁有权利对一个选择享受没有烦人后果的快乐的人,或者一个选择避免没有结果快乐的痛苦的人,挑毛病呢?
在非故意的情况下,除了在铜酸盐中的腐败行为外,其他腐败行为和犯罪行为都会导致暴力行为,同样的罪过也必须是出于对动物的同情、对劳动的同情和对他人的同情临时南自由工会