IOS之WebViewJavascriptBridge补充

上篇讲到ios中页面使用WebViewJavascriptBridge交互数据,只提供一个页面上一个按钮,如果是个列表页有多个按钮都要交互数据呢?写法要改一下:

首先要初始化

var bridge;
document.addEventListener(‘WebViewJavascriptBridgeReady’, onBridgeReady, false)
function onBridgeReady(event) {
bridge = event.bridge

bridge.init(function(message, responseCallback) {
var data = { ‘Javascript Responds’:’Wee!’ }
responseCallback(data)
})

bridge.registerHandler(‘testJavascriptHandler’, function(data, responseCallback) {
var responseData = { ‘Javascript Says’:’Right back atcha!’ }
responseCallback(responseData)
})

}

然后在每个按钮后绑定事件

<a id=”a_1″>touch1</a>
var obj = document.getElementById(‘a_1’);
obj.ontouchstart = function(e) {
e.preventDefault()
bridge.send(‘data’, function(responseData) {

})
}
<a id=”a_2″>touch2</a>
var obj = document.getElementById(‘a_2’);
obj.ontouchstart = function(e) {
e.preventDefault()
bridge.send(‘data’, function(responseData) {

})
}

发表评论