国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS中的webView加載HTML

2020-07-26 03:28:37
字體:
來源:轉載
供稿:網友

 在日常開發中,我們為了效率會用到很多很多的WebView,比如在做某個明細頁面的時候我們返回給你的可能是一個html字符串,我們就需要將當前字符串展示到webView上面,所以我們對HTML標簽需要有一定的認識,下面我們來一起用html標簽和JS寫一個打地鼠游戲,這里我們主要講解HTML標簽的書寫,只要如何和webView適配涉及到響應式布局我們下次講解:

1、首先我們先新建一個html文件


2 完整html標簽并且設置編碼格式為UTF-8


3 在body里面增加十只老鼠圖片,并且增加點擊事件,當點擊老鼠后觸發JS函數onclick="addScore(this);",代碼如下:

<img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:100px;top:200px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:200px;top:280px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:150px;top:100px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:300px;top:120px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:400px;top:200px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:600px;top:250px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:670px;top:100px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:490px;top:60px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:590px;top:30px;display:none' onclick="addScore(this);"/><img src='http://pic1.nipic.com/2008-10-16/200810169229387_2.jpg' style='width:80px;height:80px;position:absolute;left:650px;top:300px;display:none' onclick="addScore(this);"/> 

4 先將所有的老鼠圖片放入數組中,然后開啟定時器,每秒調用兩次該方法,并且隨機顯示八個老鼠圖片

//展示老鼠function showMouse(){//隱藏所有的老鼠hideAll();//控制點擊次數times++;//超過20此結束點擊if(times>20){window.clearInterval(timer);alert("游戲結束,得分"+score+"分");return;}//獲取所有的老鼠var imgs=document.getElementsByTagName("img");//隨機的顯示八只老鼠for(var i=0;i<8;i++){var tem=Math.random()*10;tem= Math.round(tem);var node=imgs[tem]node.style.display="";}}//隱藏所有老鼠function hideAll(){var imgs=document.getElementsByTagName("img");for(var i=0 ;i<imgs.length;i++){var tem=imgs[i];tem.style.display="none";}}

5 每次點擊我們需要隱藏當前的圖片,并且增加分數,每點擊一次老鼠增加一分

//增加分數function addScore(cell){cell.style.display="none";score++;document.getElementById("label").innerHTML=score+"分數";} 

6 html加載到webView中顯示

UIWebView * web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];NSString * path=[[NSBundle mainBundle] pathForResource:@"mouse.html" ofType:nil];NSData * data=[NSData dataWithContentsOfFile:path];NSString * str=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];web.scalesPageToFit=YES;[web loadHTMLString:str baseURL:nil];[self.view addSubview:web];

以上所述是小編給大家介紹的IOS中的webView加載HTML的相關知識,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大新县| 重庆市| 鸡东县| 海门市| 宁晋县| 商洛市| 上林县| 大埔区| 南通市| 长丰县| 永宁县| 清水河县| 襄汾县| 京山县| 阳江市| 宣恩县| 茌平县| 海阳市| 泰安市| 溧阳市| 育儿| 梁平县| 广州市| 福清市| 油尖旺区| 白山市| 修水县| 巴南区| 布尔津县| 安图县| 胶南市| 临颍县| 木兰县| 米脂县| 临夏县| 大悟县| 秦皇岛市| 托里县| 陵川县| 黄冈市| 威信县|