[英]Get Attribute From Multiple Div Elements into my iOS app from UIWebView W/Swift
如何从UIWebView中定义的div元素获取属性到iOS应用程序?
为了进一步解释...
我有一个非常简单的HTML文档,我将其加载到UIWebView中。 它允许用户单击以打开或关闭按钮。 我将div上的属性设置为true或false。 请参阅下面的代码。
<html>
<head>
<title>Tire Selection Template</title>
<style>
.front_truck_box{
display:flex;
flex-wrap: wrap;
border:1px solid black;
height:80px;
flex: 0 1 80px;
padding:10px;
}
.middle_truck_box, .back_truck_box {
display:flex;
flex-wrap: wrap;
border:1px solid black;
height:80px;
flex: 1 1 120px;
max-width:250px;
padding:10px;
}
.wrapper{
display:flex;
flex-direction: row;
flex-wrap: nowrap;
align-items:center;
justify-content: center;
}
.tire_box{
flex: 0 0 10px;
height:30px;
width:10px;
border:1px solid black;
cursor:pointer;
}
.tire_set{
display: flex;
flex: 0 0 35px;
justify-content: space-around;
}
.front_tire_set{
display:flex;
flex: 0 1 100%;
justify-content: space-around;
}
.first_row,.second_row{
display:flex;
flex: 1 0 100%;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
justify-content: space-around;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="front_truck_box">
<div class="first_row">
<div class="front_tire_set">
<div class="tire_box" tire="1" active="false"></div>
<div class="tire_box" tire="2" active="false"></div>
</div>
</div>
<div class="second_row">
<div class="front_tire_set">
<div class="tire_box" tire="3" active="false"></div>
<div class="tire_box" tire="4" active="false"></div>
</div>
</div>
</div>
<div class="middle_truck_box">
<div class="first_row">
<div class="tire_set">
<div class="tire_box" tire="5" active="false"></div>
<div class="tire_box" tire="6" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="7" active="false"></div>
<div class="tire_box" tire="8" active="false"></div>
</div>
</div>
<div class="second_row">
<div class="tire_set">
<div class="tire_box" tire="9" active="false"></div>
<div class="tire_box" tire="10" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="11" active="false"></div>
<div class="tire_box" tire="12" active="false"></div>
</div>
</div>
</div>
<div class="back_truck_box">
<div class="first_row">
<div class="tire_set">
<div class="tire_box" tire="13" active="false"></div>
<div class="tire_box" tire="14" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="15" active="false"></div>
<div class="tire_box" tire="16" active="false"></div>
</div>
</div>
<div class="second_row">
<div class="tire_set">
<div class="tire_box" tire="17" active="false"></div>
<div class="tire_box" tire="18" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="19" active="false"></div>
<div class="tire_box" tire="20" active="false"></div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$(".wrapper").on('click', '.tire_box', function() {
var tire = $(this).attr("tire");
var active = $(this).attr("active");
console.log(active);
//console.log(tire);
if( active == "false"){
$(this).css("background-color","black");
$(this).attr("active","true");
}else{
$(this).css("background-color","white");
$(this).attr("active","false");
}
});
function test(){
return "test"
}
});
</script>
</body>
</html>
您可以在这里查看其运行情况: https : //jsfiddle.net/x11joex11/0d92ao80/1/
我知道下面的代码行很快。
theWebView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")
该行将返回所有HTML。 现在我也许可以分析一下,但是我想应该有可能,因为我正在使用jquery运行命令来获取所有被单击的框。 单击每个框后,我将其“活动”属性更改为“真”或“假”。 我该怎么做呢?
例如,返回一个在单击的每个div (.tire_box)上具有“轮胎”属性值的数组 ( “ active = true” )。
使用jQuery(因为我的HTML上有它)或Javascript的答案是可以的。 只是不确定要在stringByEvaluatingJavascriptFromString()
函数中放入什么
更新::
使用此命令,我能够从一个轮胎中获得价值。
document.getElementsByClassName('tire_box')[0].getAttribute('tire')
我需要能够选择轮胎列表,并以某种方式快速地对其进行处理。 尚不确定执行此操作的最佳方法...
有趣的是,我似乎能够运行jQuery代码。 我在javascript外部的变量中创建了函数test(),并在html中添加了alert('test')并运行了此代码...
我运行的Swift Javascript
$(document).ready(function(){
test();
});
在document.ready()外部添加到HTML
var test = function(){
alert("test function ran");
return "test";
};
在stringByEvaluatingJavaScriptFromString函数上,它调用了警报,但是我似乎没有从stringByEvaluatingJavaScriptFromString函数得到任何结果...只是空白?
我想知道函数“如何”返回javascript,我必须在javascript命令中做什么才能使其返回某些信息?
经过大量研究,我终于找到了答案。 事实证明,stringByEvaluatingJavaScriptFromString将返回要运行的LAST语句的结果。
也就是说,我在下面修改了我的html。
<html>
<head>
<title>Tire Selection Template</title>
<style>
.front_truck_box{
display:flex;
flex-wrap: wrap;
border:1px solid black;
height:80px;
flex: 0 1 80px;
padding:10px;
}
.middle_truck_box, .back_truck_box {
display:flex;
flex-wrap: wrap;
border:1px solid black;
height:80px;
flex: 1 1 120px;
max-width:250px;
padding:10px;
}
.wrapper{
display:flex;
flex-direction: row;
flex-wrap: nowrap;
align-items:center;
justify-content: center;
}
.tire_box{
flex: 0 0 10px;
height:30px;
width:10px;
border:1px solid black;
cursor:pointer;
}
.tire_set{
display: flex;
flex: 0 0 35px;
justify-content: space-around;
}
.front_tire_set{
display:flex;
flex: 0 1 100%;
justify-content: space-around;
}
.first_row,.second_row{
display:flex;
flex: 1 0 100%;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
justify-content: space-around;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="front_truck_box">
<div class="first_row">
<div class="front_tire_set">
<div class="tire_box" tire="1" active="false"></div>
<div class="tire_box" tire="2" active="false"></div>
</div>
</div>
<div class="second_row">
<div class="front_tire_set">
<div class="tire_box" tire="3" active="false"></div>
<div class="tire_box" tire="4" active="false"></div>
</div>
</div>
</div>
<div class="middle_truck_box">
<div class="first_row">
<div class="tire_set">
<div class="tire_box" tire="5" active="false"></div>
<div class="tire_box" tire="6" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="7" active="false"></div>
<div class="tire_box" tire="8" active="false"></div>
</div>
</div>
<div class="second_row">
<div class="tire_set">
<div class="tire_box" tire="9" active="false"></div>
<div class="tire_box" tire="10" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="11" active="false"></div>
<div class="tire_box" tire="12" active="false"></div>
</div>
</div>
</div>
<div class="back_truck_box">
<div class="first_row">
<div class="tire_set">
<div class="tire_box" tire="13" active="false"></div>
<div class="tire_box" tire="14" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="15" active="false"></div>
<div class="tire_box" tire="16" active="false"></div>
</div>
</div>
<div class="second_row">
<div class="tire_set">
<div class="tire_box" tire="17" active="false"></div>
<div class="tire_box" tire="18" active="false"></div>
</div>
<div class="tire_set">
<div class="tire_box" tire="19" active="false"></div>
<div class="tire_box" tire="20" active="false"></div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script>
var returnToIOS = function(){
//Calculate which tires are 'active=true' and 'active=false' to the array.
var tires = [];
var tireObj = {};
$(".tire_box").each(function(index){
tireObj = {};//create new object.
tireObj["number"] = $(this).attr("tire");
tireObj["active"] = $(this).attr("active");
tires.push(tireObj);//add to our tire array the object.
});
var jsonString = JSON.stringify(tires);
return jsonString;
};
$(document).ready(function(){
$(".wrapper").on('click', '.tire_box', function() {
var tire = $(this).attr("tire");
var active = $(this).attr("active");
if( active == "false"){
$(this).css("background-color","black");
$(this).attr("active","true");
}else{
$(this).css("background-color","white");
$(this).attr("active","false");
}
});
});
</script>
</body>
</html>
注意函数returnToIOS 。 我做到了,所以它将返回一个JSON字符串,其中包含我想要的模板数据。
接下来在Swift中,我执行了以下代码。
let javascript = "returnToIOS();"
let value = self.theWebView.stringByEvaluatingJavaScriptFromString(javascript)
这将返回带有所有数据的JSON字符串!
为了使它变得更好,我在我的iOS项目中包括了一个名为Swift Json( https://github.com/dankogai/swift-json/ )的库。
然后,我运行这些命令。
let data = value!.dataUsingEncoding(NSUTF8StringEncoding)
let json = JSON(data:data!)
for tires in json.asArray! {
print(tires["number"].asString!+" : "+tires["active"].asString!)
}
和Wah La,我所有的数据都放在一个可以处理的漂亮数组中!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.