更新时间:2022-05-04 23:23:37
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();
}
稍微解释下,这里在点击之后再remove可能有的人会有疑问,既然跳转了为啥还要remove。这是因为我这里使用了sui的路由功能,跳转之后缓存资源不刷新。
这段代码在ios端不能正常运行报一个这样的错误:
click()
function is only defined for HTMLInputElement
”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();
}
然而并不好使,起初我是怀疑这个改动不对,但是后来发现不是这样的,在IOS里click的鼠标事件应该有touchstart,touchmove、touchend等触摸事件或者他们的高级事件来触发点击屏幕这个事件。所以我又作出如下改动: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="";
}
这些方法写的非常初级,仅供思路参考,表喷我哦。
这里加三个方法在html上,主要是为了规避由于路由问题从别的界面切换过来的时候,事件绑定不上的问题。