gpt4 book ai didi

javascript - 从 UIWebView W/Swift 将多个 Div 元素的属性获取到我的 iOS 应用程序中

转载 作者:行者123 更新时间:2023-11-30 13:28:39 24 4
gpt4 key购买 nike

如何从 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/

我知道以下 swift 代码行。

theWebView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")

该行将返回所有 HTML。现在我也许可以完成这个并解析,但我认为这应该是可能的,因为我正在使用 jquery 运行命令来获取所有被单击的框。单击每个框时,我将其“事件”属性更改为“true”或“false”。 我该怎么做?

例如,在单击的每个 div (.tire_box) 上返回一个包含“tire”属性值的数组(“active=true “)。

使用 jQuery(因为我的 HTML 上有它)或 Javascript 的答案是可以的。只是不确定要在 stringByEvaluatingJavascriptFromString() 函数中放入什么

更新::

我能够使用此命令从“一个”轮胎获取值。

document.getElementsByClassName('tire_box')[0].getAttribute('tire')

我需要能够获得所选轮胎的列表,并以某种方式快速地用它做一些事情。还不确定执行此操作的最佳方法...

有趣的是我似乎能够运行 jQuery 代码。我在 javascript 外部的变量中创建了一个函数 test() ,并在我的 html 中创建了一个警报('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!)
}

Wha La 我将所有数据都放在一个漂亮的数组中,我可以用它进行操作!

关于javascript - 从 UIWebView W/Swift 将多个 Div 元素的属性获取到我的 iOS 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36840196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com