简体   繁体   English

UIWebView中的Click事件不起作用

[英]Click event in UIWebView doesn't work

This is the script that I serve into a webpage: 这是我投放到网页中的脚本:

var myDiv = document.createElement('div');
var style = myDiv.style;
style.opacity = 0;
style.zIndex = 10000;
style.position = 'absolute';
style.top = '0px';
style.left = '0px';
style.width = '100%';
style.height = '100%';
myDiv.addEventListener("click", function(event) {
    new Image().src="http://myhostna.me/something.png?CLICKED"+Math.random(); 
});
document.body.appendChild(myDiv);

When I serve this script in a webpage for a WebView on Android, I know that the click event fired because I can see the GET request for something.png?CLICKED in my access logs. 当我在Android上用于WebView的网页中提供此脚本时,我知道单击事件已触发,因为我可以在访问日志中看到对something.png?CLICKED的GET请求。

The problem is that I don't see those requests when I serve the script into a webpage for a UIWebView on iOS. 问题是,当我将脚本提供给iOS上的UIWebView的网页时,我看不到那些请求。

Does anyone know why that might be? 有谁知道为什么会这样吗?

These webviews I'm getting served into are third party, and I don't know anything about what kinds of options they enable or disable. 我进入的这些网络视图是第三方的,我对它们启用或禁用的选项种类一无所知。

Please change 请更换

new Image().src="http://myhostna.me/something.png?CLICKED"

to

new Image().src="http://myhostna.me/something.png?CLICKED"+Math.random();

Edit: 编辑:

please add these lines to your code: (it can be after the line style.zIndex = 1000;) 请将这些行添加到您的代码中:(可以在style.zIndex = 1000之后;)

style.width='500px';
style.height='500px';
style.background='red';
myDiv.innerHTML = 'my div';

and change 并改变

style.opacity = 0;

to

style.opacity = 1;

(it's easier to debug this way) (以这种方式进行调试更容易)

Also keep the alert('test'); 同时保持警报(“测试”); inside the eventlistener function 在事件监听器函数中

You should see the alert box if everything is successfully executed. 如果一切都成功执行,您应该看到警报框。


It looks like the problem is your div does not have a size (unless you set it with CSS somewhere else) 看来问题在于您的div没有大小(除非您在其他地方使用CSS设置了大小)


new idea for debugging: 调试的新思路:

add this to the script 将此添加到脚本

myDiv.id = 'myDiv';

document.body.addEventListener("touchstart", function(event) {
    alert(event.target.id+' '+event.target.tagName);
}, false);

Then, when you click on your div tag, you should see 'myDiv DIV' in an alert box. 然后,当您单击div标签时,您应该在警报框中看到“ myDiv DIV”。 If you see other id (or empty string, since some page elements may not have an id attribute), that means you are not actually clicking on the div you are expecting. 如果看到其他id(或空字符串,因为某些页面元素可能没有id属性),则意味着您实际上并没有点击期望的div。 Then it could be a z-index related issue. 那么这可能是与z-index相关的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM