【问题标题】:How to Call function in the webview html href onClick in appcelerator titanium如何在 appcelerator Titan 中调用 webview html href onClick 中的函数
【发布时间】:2016-05-09 06:24:55
【问题描述】:

我在 html 中有 webview,我在每个链接中显示了多个链接,我想调用 javascript 函数。但是 javascript 函数没有调用,请帮助解决这个问题。提前致谢。 下面是我的代码。

var win = Ti.UI.createWindow({
  backgroundColor : '#fff'
});
var webViewTesting = Ti.UI.createWebView({
  height : Ti.UI.SIZE,
  width : Ti.UI.FILL,
  html : "<html><body><p>Hi Nice Hair Style By <a href='#' onclick='displayUserDetails();'>Raju</a><a href='#' onclick='displayUserDetails();'>Jon Hills</a></body></html>",
});

win.add(webViewTesting);

function  displayUserDetails(){
  Ti.API.info('displayUserDetails');    
 }

【问题讨论】:

    标签: function webview href appcelerator appcelerator-titanium


    【解决方案1】:

    您可以在 .html 中使用Ti.App.fireEvent

    <a href="#" onclick="Ti.App.fireEvent('action');">Test</a>
    

    然后,例如在您的 index.js 中:

    Ti.App.addEventListener('action', function(){
        //do something
    });
    

    编辑,将您的代码更改为:

    var win = Ti.UI.createWindow({
      backgroundColor : '#fff'
    });
    var webViewTesting = Ti.UI.createWebView({
      height : Ti.UI.SIZE,
      width : Ti.UI.FILL,
      html : '<html><body><p>Hi Nice Hair Style By <a href="#" onclick="Ti.App.fireEvent('displayUserDetails');">Raju</a><a href="#" onclick="Ti.App.fireEvent('displayUserDetails');">Jon Hills</a></body></html>"
    });
    
    win.add(webViewTesting);
    
    Ti.App.addEventListener('displayUserDetails', function(){
      //do something
      Ti.API.info("displayUserDetails');    
    });
    

    【讨论】:

    • 您好 Thomas,感谢您的快速回复,我已经尝试了上述方法,但是在我的 index.js 中,我的 js 文件中没有触发火灾事件侦听器,请帮助我。
    • 是否有可能在 index.js 文件中我有 webview 我正在使用 html 属性,因为我在每个要调用函数的链接中有多个 href 链接
    • 我的答案改了,你可以试试我贴的新代码吗?
    猜你喜欢
    • 2021-07-30
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    相关资源
    最近更新 更多