在 webview 代码中,它调用加载的 javascript,传入 html div 元素 id `"module_1", 的字符串<div id="module_1" class="module"></div>
:
my_web_view.evaluateJavascript(
"javascript:sendHtmlMarkerLocation("module_1");", null)
并且 javascript 将接受 div 元素 id 的字符串,然后查找 div 元素并获取其位置:
function sendHtmlMarkerLocation( moduleElementId ) {
console.log('+++ enter sendHtmlMarkerLocation()'+moduleElementId+',
window.androidObj:'+window.androidObj);
var elm = null
var moduleId = moduleElementId
if (moduleElementId instanceof String) {
elm = document.getElementById(moduleElementId)
} else if (moduleElementId instanceof Element) { // how it changes to the div element ???
elm = moduleElementId
moduleId = elm.id
}
var _x = 0;
var _y = 0;
while( elm && !isNaN( elm.offsetLeft ) && !isNaN( elm.offsetTop ) ) {
_x += elm.offsetLeft - elm.scrollLeft;
_y += elm.offsetTop - elm.scrollTop;
elm = elm.offsetParent;
}
var width = getWindowWidth()
var height = getWindowHeight()
window.androidObj.sendLocationToAndroid(moduleId, _x, _y, width, height);
}
但它崩溃了,因为传入的moduleElementId
不再是字符串 div id "module_1"
,而是解析为<div id="module_1" class="module"></div>
.
对应的 console.log 是:
+++ enter sendHtmlMarkerLocation()[object HTMLDivElement], window.androidObj:function AndroidClass(){} @ 48
它如何从字符串 id 更改为<div>
元素?