javascript中的javascript变为onClick

时间:2016-01-15 14:25:29

标签: javascript json

我有一个var我需要作为AS3的JSON字符串发送。它必须在链接中打印出来。如何让我的var出现在html的onClick中,如下所示?

   <script>
    var NeedMyVarHere = {
    One:"This is var one!",
    Two:"This is JSON Object 2"
                };
      NeedMyVarHere = JSON.stringify(NeedMyVarHere);
     </script>


 <a href="javascript:void(0);" onclick="AirBridge.evoke('myCustomFunction(NeedMyVarHere)')" return false;>Testing!</a>

更新以澄清。

AS要求发送&#34;字符串&#34;但您可以在字符串中声明自定义函数。我没有写这门课。我只需要在我的onClick中使用String格式编写的JSON对象。像这样:

onClick="AirBridge.evoke('myCustomFunction({One:"This is var one!",Two:"This is var Two"})"

但我希望使用单个var来打印出来......就像......

onClick="AirBridge.evoke('myCustomFunction(NeedMyVarHere)"

我希望这是有道理的。

4 个答案:

答案 0 :(得分:0)

最好使用unobtrusive使用addEventListener JavaScript。但对于这个解决方案,我会说:

onclick="AirBridge.evoke('myCustomFunction(NeedMyVarHere)')" return false;
//---------------------------------------------------------^ " prematurely closed.

更改为:

onclick="AirBridge.evoke('myCustomFunction(' + NeedMyVarHere.One + ')'); return false;"

或者:

onclick="AirBridge.evoke('myCustomFunction(NeedMyVarHere)'); return false;"

答案 1 :(得分:0)

你能不做下面图解的事情吗?

<script>
    var NeedMyVarHere = {
        One:"This is var one!",
        Two:"This is JSON Object 2"
    };
    NeedMyVarHere = JSON.stringify(NeedMyVarHere);
    function onClickFunction() {
        Airbridge.evoke(myCustomFunction(NeedMyVarHere));
    }
 </script>
 <a href="javascript:void(0);" onclick="onClickFunction()">Testing!</a>

这样onClick函数不需要接受参数,但可以调用无参数函数'onClickFunction',然后调用你想要调用的函数(带参数)。

答案 2 :(得分:0)

这里详细解释https://github.com/myflashlab/webView-ANE/issues/48但是,这里有一份副本:

在你的JS中,创建一个按钮或链接来调用JS函数

def main(): 
encoded = "
غيعاژ هرظف
 "

english_frequences = [
    14, 4.2, o.7, 5.2, 0.1, 1.2, 0.4,
    1, 1.4, 7.5, 0.1, 8.5, 2.1, 0.1,
    3.3, 2.6, 0.7, 0.3, 0.6, 0.2, 1.5,
    0.2, 1.6, 1.2, 3, 1.7, 2.7, 5.7, 7.1, 6, 5.7, 9.1]

(key, decoded) = vigenere_decrypt(english_frequences, encoded)

在您的js代码中,创建您的Json并将其发送回Air,如下所示:

<button type="button" onClick="sendComplexDataToAir()">Send Complex Data To Air</button>
然后,就像这样

在Air方面接收它
function sendComplexDataToAir()
{
    var myJSONObject = 
    {
        "someData": 
        [
            {"var1": "value 1"},
            {"var2": "value 2"},
            {"var3": "value 3"},
            {"var3": "As long as you have a valid Json in JS, you can have it received on the Air side with no problem!"}
        ]
    };

    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify  
    var jsonStr = JSON.stringify(myJSONObject);

    // http://www.w3schools.com/jsref/jsref_encodeURI.asp
    var encodedStr = encodeURIComponent(jsonStr);

    // and finally send it to flash
    AirBridge.evoke('parseJson('+ encodedStr +')');
}

答案 3 :(得分:-1)

您的代码存在的问题是它没有将引用传递给变量,因为它只是一个硬编码字符串。因为它需要是一个字符串,所以最好使用addEventListener并构建字符串而不是内联事件。

<a href="javascript:void(0);" id="myLink">xxxx</a>

和脚本

document.getElementById("myLink").addEventListener("click", function (e) {
    AirBridge.evoke("myCustomFunction('" + JSON.stringify(NeedMyVarHere) + "')");
    return false;   // or e.preventDefault();
}); 

确保在将元素添加到页面之后运行此行(文档就绪,onload或仅在元素之后的正文中。)