[英]passing parameter to javascript method
i have html code like: 我有这样的html代码:
<button type="button" class="button button-raised larger" onclick="app.shareOnIg();">Share on Instagram</button>
</div>
<script type="text/javascript">
app.initialize();
</script>
js looks like: js看起来像:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
shareOnIg: function () {
Instagram.share('picture', 'example', function (err) {
});
}
};
I want to replace example with actual value dynamically. 我想用实际值动态替换示例。 plz advise how.
请告知如何。 I tried to add a parameter in the function and modified the call like
我试图在函数中添加参数并修改了调用
<button type="button" class="button button-raised larger" onclick="app.shareOnIg(" + myparam + ");">Share on Instagram</button>
</div>
but this did not work 但这没有用
var app = { shareOnIg: function (value) { console.log(value); Instagram.share('picture', value, function (err) { }); } };
<button type="button" class="button button-raised larger" onclick="app.shareOnIg( 'This is the passed Value');">Share on Instagram</button>
In this case all is correct. 在这种情况下,一切都是正确的。 One problem is the how is passed the variable
myparam
. 一个问题是如何传递变量
myparam
。 If the variable is global, we can little modify the code to: 如果变量是全局变量,我们几乎可以将代码修改为:
<button type="button" class="button button-raised larger" onclick="app.shareOnIg(myparam);">Share on Instagram</button>
</div>
just change: 只是改变:
onclick="app.shareOnIg(" + myparam + ");"
to: 至:
onclick="app.shareOnIg(myparam);"
It seems you need to pass an argument to shareOnIg
function 看来您需要将参数传递给
shareOnIg
函数
shareOnIg: function (data) {
// console.log(data) here to see the parameter passed from the thml
Instagram.share('picture', 'example', function (err) {});
}
Instead go unobtrusive: 取而代之的是:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
var btns = document.querySelectorAll('.button');
document.addEventListener('deviceready', this.onDeviceReady, false);
btns.forEach((btn)=>btn.addEventListener('click', this.shareOnIg.bind(myparam)));
},
shareOnIg: function(param) {
Instagram.share('picture', 'example', function(err) {});
}
};
What about this? 那这个呢?
<input type="text" value="John" id="name"> <button onclick="sayHi('name')">Say Hi</button> <script> function sayHi(control) { var e = document.getElementById(control); console.log(e.value); } </script>
You will find it much easier in the long run to dispense with onclick
attributes and to write proper event handlers. 从长远来看,您会发现,省去
onclick
属性并编写适当的事件处理程序会容易得多。 Below is an example of how it can be done. 以下是如何完成此操作的示例。
<button type="button" id="doit" … > … </button>
document.addEventListener("DOMContentLoaded",function() {
var button=document.querySelector('button#doit');
button.addEventListener('click',function() {
app.shareOnIg(myparam);
}, false);
}, false);
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
shareOnIg: function (something) { // new parameter
Instagram.share('picture', something, function (err) {
}
}
};
The solution to your question is to add a parameter variable in your shareOnIg
method, and call the method when the tmie comes. 您问题的解决方案是在
shareOnIg
方法中添加一个参数变量,并在shareOnIg
时调用该方法。
In the above rewrite, I have 在上面的重写中,我有
button
an id
to make it findable button
一个id
使其可以被找到 DOMContentLoaded
event which triggers when the page has loaded enough for the DOM to be ready DOMContentLoaded
事件添加了一些行为,该事件在页面已加载足够的DOM准备就绪时触发 id
and attached a function to its click
event. id
找到了按钮,并将函数附加到其click
事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.