javascript var into onClick in html
javascript var into onClick in html
我有一个变量,我需要作为AS3的JSON字符串发送。它必须按照链接中的打印方式发送。如何让我的变量出现在 html 的点击中,如下所示?
<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 需要"发送字符串",但您可以在字符串中声明自定义函数。这门课不是我写的。我只需要在我的onClick中以字符串格式编写一个JSON对象。喜欢这个:
onClick="AirBridge.evoke('myCustomFunction({One:"This is var one!",Two:"This is var Two"})"
但我希望使用单个 var 将其打印出来,例如......
onClick="AirBridge.evoke('myCustomFunction(NeedMyVarHere)"
我希望这是有道理的。
这在 https://github.com/myflashlab/webView-ANE/issues/48 这里详细解释,但这是一个副本:
在 JS 中,创建一个按钮或链接来调用 JS 函数
<button type="button" onClick="sendComplexDataToAir()">Send Complex Data To Air</button>
在你的 js 代码中,创建你的 JSON 并将其发送回 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 +')');
}
然后,像这样在空气侧接收它
private function onReceivedMassage(e:RichWebViewEvent):void
{
DynamicFunc.run(this, e.param);
}
public function parseJson($str:String):void
{
trace(decodeURIComponent($str));
}
最好使用 addEventListener
unobtrusive
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;"
你能不做下面这样的事情吗?
<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",然后调用您首先要调用的函数(带有参数)。
代码的问题在于它没有传递对变量的引用,因为它只是一个硬编码的字符串。由于它需要是一个字符串,因此最好使用 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();
});
确保此行在元素添加到页面后运行(文档就绪、加载或仅在元素之后的正文中运行。
相关文章:
- lightbox in html 5 and javascript
- Hidding TextBox in html
- android spinner in html/javascript
- 如何从 javascript in html 访问 Java 中的方法
- Traversal in html dom with jquery
- Displaying php results in html <div>?
- Indy TIdHTTPServer OnCommandGet javascript in html not execu
- javascript var into onClick in html
- JavaScript in HTML head, innerhtml 不起作用
- Mustache using Partial Command in HTML & jQuery
- J'avaScript not in HTML
- How to keep the values on a registration form in html &
- jquery in html injection
- javascript onclick in html confusion
- Javascript conflict in Html & CSS
- textbox in html
- z-index and onclick in HTML
- javascript in html
- <a> tag in html
- lnk with database with ajax in html