是否可以使用代码创建html元素 添加进index.html里
没试过没试过
因为webView是最顶层。估计加了也是没用。要是可以,那我加载完网页,再创建btton add到parent 岂不是可以
不一样的, webview实际是html元素,并不在canvas里,它是一个 iframe,所以创建一个元素层级在 iframe上就可以的
怎么创建是关键哈哈
我刚才截图的就是在creator里试过的,web里指定好用,native就不大知道了
求个案例demo
var btn = document.createElement(‘div’);
btn.style.position = "fixed"
btn.style.top = "20px"
btn.style.left = "20px"
btn.innerHTML = '返回';
var main = document.getElementById('app');
main.appendChild(btn);
我试试,手机上可否
好的,希望能帮到你
如果可以大致思路就是这样 也可以先创建,然后控制它的display
感觉可行 多谢多谢
已解决:
export default class UIH5 extends UIBase {
@property(cc.Node)
h5Game: cc.Node = null;//webView所在节点
initUI() {
this.onClick("_close", this.onBtnClose);
this.h5Game.on('loaded', this.onWebViewLoaded, this);
}
onWebViewLoaded() {
let self = this;
AppLog.log("加载完成onWebViewLoaded 创建一个btn 返回")
var btn = document.createElement("div");
btn.style.position = "fixed"
btn.style.color = "red"
btn.style.top = "20px"
btn.style.left = "20px"
btn.innerHTML = '返回';
var main = document.getElementById('GameDiv');
main.appendChild(btn);
btn.onclick = (function (param) {
// parent.postMessage("------------hello!-----cocos---------", "*")
self.closeUI();//关闭UI
main.removeChild(btn);//移除按钮
})
}
onBtnClose() {
this.closeUI();
}
private item;
onOpenUI(param) {
this.item = param.p;
this.initWeb();
}
initWeb() {
// 这里是与内部页面约定的关键字,请不要使用大写字符,会导致 location 无法正确识别。
var scheme = "testkey";
//这里是移动端, 接收web传过来的消息
function jsCallback(target, url) {
// 这里的返回值是内部页面的 URL 数值,需要自行解析自己需要的数据。
var str = url.replace(scheme + '://', ''); // str === 'a=1&b=2'
// webview target
console.log("jsCallback-------str-------", str);
window["closeWebView"](target, url);
}
//web
window["closeWebView"] = this.closeWebView.bind(this);
let webView = this.h5Game.getComponent(cc.WebView)
webView.url = this.item.awardDistributingParameter;
webView.setJavascriptInterfaceScheme(scheme);
webView.setOnJSCallback((sender, url) => {
this.h5Game.active = false;
})
}
closeWebView(e, url) {
this.closeUI();
}
}