先上出现问题的代码:
HTML: <div class="info_content txt-item" onclick="selectInfoById(this)"> JS: function selectInfoById(this_) { var uid = $(this_).siblings("input[name=uid]").val(); var url = "/mobile/queryInfoById?uid="+uid; var elementById = document.getElementById("jumphiddenDiv"); var aElement = document.createElement("a"); aElement.setAttribute("id","createa"); aElement.setAttribute("href",url); aElement.setAttribute("data-no-cache","true"); elementById.appendChild(aElement); jQuery("#createa")[0].click(); aElement.remove(); }
AI 代码解读
稍微解释下,这里在点击之后再remove可能有的人会有疑问,既然跳转了为啥还要remove。这是因为我这里使用了sui的路由功能,跳转之后缓存资源不刷新。
这段代码在ios端不能正常运行报一个这样的错误:
'undefined' is not a function evaluating XX.click() in Safari
这就尴尬了,赶紧上网查,查到这个东西:
https://stackoverflow.com/questions/12744202/undefined-is-not-a-function-evaluating-el-click-in-safari#
虽然没有完全看懂,但是也明白个大概齐:他说:“
the
click()
function is only defined for
HTMLInputElement
”
并给出了改进意见,根据他的改进意见作出如下改动:
然而并不好使,起初我是怀疑这个改动不对,但是后来发现不是这样的,在IOS里click的鼠标事件应该有touchstart,touchmove、touchend等触摸事件或者他们的高级事件来触发点击屏幕这个事件。所以我又作出如下改动:JS: function selectInfoById(this_) { var uid = $(this_).siblings("input[name=uid]").val(); var url = "/mobile/queryInfoById?uid="+uid; var elementById = document.getElementById("jumphiddenDiv"); var aElement = document.createElement("a"); aElement.setAttribute("id","createa"); aElement.setAttribute("href",url); aElement.setAttribute("data-no-cache","true"); elementById.appendChild(aElement); //jQuery("#createa")[0].click(); // First create an event var click_ev = document.createEvent("MouseEvents"); // initialize the event click_ev.initEvent("click", true /* bubble */, true /* cancelable */); // trigger the event document.getElementById("createa").dispatchEvent(click_ev); aElement.remove(); }
AI 代码解读
这里加三个方法在html上,主要是为了规避由于路由问题从别的界面切换过来的时候,事件绑定不上的问题。HTML:改动 <div class="info_content txt-item" onclick="selectInfoById(this)" ontouchstart = "myTouchStart(this)" ontouchend = "myTouchEnd(this)" ontouchmove="myTouchMove(this)"> JS:添加 var xtarget = ""; function myTouchStart(this_) { var e = window.event; xtarget = e.clientX; } function myTouchEnd(this_) { var e = window.event; if(e.clientX ==xtarget){ selectInfoById(this_); xtarget=""; }else{ xtarget=""; } } function myTouchMove(this_) { xtarget=""; } 这些方法写的非常初级,仅供思路参考,表喷我哦。
AI 代码解读
引用资料(非常感谢):
https://www.cnblogs.com/irelands/p/3433628.html
http://www.cnblogs.com/dolphinX/archive/2012/10/09/2717119.html
https://www.cnblogs.com/fengfan/p/4506555.html
http://blog.csdn.net/jiangcs520/article/details/17564065