javascript - 从 DIV 元素中获取值

标签 javascript java android webview

我想将 HTML 元素值传递给 Android 应用程序。我

我尝试使用以下代码检索值:

document.getElementById(\"summary\").value --> giving null value
document.getElementById(\"summary\").innerHtml --> giving null value
document.getElementById(\"summary\").textContent --> giving null value 

我正在尝试实现 javascript MutationObserver。因此每当内容发生变化时,我可以立即将值传递给 android java 代码。

谁能帮帮我?

我使用了以下代码:

    myWebView = (WebView)this.findViewById(R.id.myWebView);
    myWebView.getSettings().setJavaScriptEnabled(true);


     myWebView.loadUrl("https://helloworld.com/details");

    myWebView.addJavascriptInterface(new JavaScriptInterface(this, myWebView), "MyHandler");
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        myWebView.setWebContentsDebuggingEnabled(true);
    }

    myWebView.evaluateJavascript("(function(){return document.getElementById(\"summary\").textContent})();",
            new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String html) {
                   Log.e("HTML Content",html);
                }
            });

这是我的 HTML 代码:

 <div class="well" id="summary" *ngIf="viewModel != null">
    <div class="SuccessSummary">{{viewModel.successSummary}}</div>
 </div>

最佳答案

试试这个

myWebView.evaluateJavascript("(function(){return 
document.getElementById('summary').innerHTML})();",
        new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String html) {
               Log.e("HTML Content",html);
            }
        });

关于javascript - 从 DIV 元素中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55416177/

相关文章:

javascript - Node.js - 套接字断开连接事件无法正常工作

javascript - 在完成之前,事情发生在我的 for 循环之外(javascript)

javascript - 在 "onfocus"事件中检查 CapsLock ON

java - 去除字符串中的重复字符

java - `dispatchGesture` 忽略覆盖 View

javascript - 将对象数组转换为数组对象 : Javascript

java - 连接 MySQL 时通信链路失败

java - 为什么我应该使用单独的线程在 JAVA 中显示 GUI

android - 将监听器设置为来自 Fragment 的 Android 服务

javascript - 是否可以配置 iOS 原生 HTML5 日期选择器?