Javascript插件需要回调到客户端';s Javascript
我正在开发一个插件,该插件允许向购物车添加物品。插件是我的,购物车属于客户。这个想法是添加我的插件和几行代码来配置。 一旦购买了商品,我需要在客户页面上调用一个函数,以便将其添加到购物车中,但我没有管理。 我有以下代码:Javascript插件需要回调到客户端';s Javascript,javascript,Javascript,我正在开发一个插件,该插件允许向购物车添加物品。插件是我的,购物车属于客户。这个想法是添加我的插件和几行代码来配置。 一旦购买了商品,我需要在客户页面上调用一个函数,以便将其添加到购物车中,但我没有管理。 我有以下代码: $(文档).ready(函数(){ //plugin1.CallBackTest; }); var plugin1=新函数(){ this.CallBackTest=函数(str){ log(“类中的回调”); 功能性Wantocall(str); } } 函数Functi
$(文档).ready(函数(){
//plugin1.CallBackTest;
});
var plugin1=新函数(){
this.CallBackTest=函数(str){
log(“类中的回调”);
功能性Wantocall(str);
}
}
函数FunctionIWantToCall(str){
log(“客户端回调”+str);
}
发回
谢谢Stavros Angelis,它可以工作:
<script type="text/javascript">
$(document).ready(function () {
plugin1.CallBackFunction = "FunctionIWantToCall";
});
var plugin1 = new function () {
var myplugin = this;
this.CallBackFunction = "";
this.CallBackTest = function () {
console.log("callback in class");
var item = JSON.parse($(this).attr("vals"));
if (myplugin.CallBackFunction != "") {
window[myplugin.CallBackFunction](item);
}
}
function BindCartButtons() {
console.log("binding buttons")
$(document).on("click", ".htmlcreatedbyplugin > button", myplugin.CallBackTest);
}
BindCartButtons();
}
function FunctionIWantToCall(item) {
console.log("callback on client " + item.id);
}
</script>
<div class="htmlcreatedbyplugin">
<button type="button" vals="{"id":12345, "color":"blue"}">Buy Me</button>
</div>
$(文档).ready(函数(){
plugin1.CallBackFunction=“FunctionIWantToCall”;
});
var plugin1=新函数(){
var myplugin=this;
this.CallBackFunction=“”;
this.CallBackTest=函数(){
log(“类中的回调”);
var item=JSON.parse($(this.attr(“vals”));
if(myplugin.CallBackFunction!=“”){
窗口[myplugin.CallBackFunction](项目);
}
}
函数BindCartButtons(){
console.log(“绑定按钮”)
$(document).on(“单击“,”.htmlcreatedbyplugin>按钮”,myplugin.CallBackTest);
}
BindCartButtons();
}
函数FunctionIWantToCall(项目){
log(“客户机上的回调”+item.id);
}
买我
我发现你的解释有点让人困惑,所以我不确定我在这里的思路是否正确,这就是为什么我要评论而不是回答。我将通过jquery:$(document)捕获整个单击事件;然后,我将传递我需要的变量作为数据属性或隐藏输入,以便我的函数在DOM中查找。如果这是在正确的轨道上,您可以让我更清楚地了解您想要什么,我会找到一个更好更完整的答案。这可能会起作用,当然有许多按钮(每个产品一个),但我认为这应该不是一个问题,我可以使用数据属性作为值。