【问题标题】:Navigate within webpage in WebView control在 WebView 控件中的网页内导航
【发布时间】:2014-05-08 19:30:52
【问题描述】:

我正在尝试在我的 WebView 控件(Cocoa 应用程序)中从远程服务器加载的网页中导航。我想导航到我可以在该页面的 HTML 代码中看到的特定标签。这一切的目的是在 WebView 控件的顶部显示我感兴趣的 HTML 页面部分。

我知道在 HTML 代码中,您可以使用 #MIDDLE、#TOP 等内容进行导航。但是,这是否可以使用 WebView API 从 HTML 代码之外进行?

感谢您提前回复!

【问题讨论】:

    标签: html cocoa webkit


    【解决方案1】:

    我在另一个问题 (How to scroll HTML page to given anchor using jQuery or Javascript?) 的帮助下找到了我的问题的答案。

    下面的代码对我有用。它在 WebView 组件 self.webView 中加载的 HTML 数据中搜索具有属性:class= "container" 的 HTML 元素。

    -(void) scrollMyImportantHTMLPartInView
    {
        // Get a list of HTML elements that contain attribute class = "container" (eg. <div class "container">)
        DOMNodeList *nodeList = [[[self.webView mainFrame] DOMDocument] getElementsByClassName: @"container"];
        if( nodeList && nodeList.length >= 1 ) {
    
            // get the first node (class = "container") from the list
            DOMNode *domNode = [nodeList item:0];
    
            // Make sure it's a DOM element type
            if( domNode.nodeType == DOM_ELEMENT_NODE ) {
    
                // It's now save to cast from DOMNode* to DOMElement*
                DOMElement* domElement = (DOMElement*) domNode;
    
                // Scroll begining of HTML node into view
                [domElement scrollIntoView: YES];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多