2011-01-20 36 views
0

我在頁面中嵌入了一個ActiveX控件,它與打印機和LED顯示器進行通信。有沒有辦法使用jQuery訪問AX控件中可用的方法?例如:如何使用jQuery訪問嵌入式ActiveX控件的方法?

$("#plugin").updateDisplay("COM6:", "test"); 
$("#plugin").printReceipt("COM5:", "\x0a\x0a\x1dV\x42\x00"); 
$("#plugin").openDrawer(); 

我知道上述不起作用,但是有沒有一些類似的方法做相當於使用jQuery?

此外,嵌入式代碼看起來這樣:

<object id="plugin" type="application/x-ticket" width="1" height="1"> 
<param name="onload" value="pluginLoaded" /></object> 

我可以訪問使用JavaScript的jQuery外的方法,但我想也許有訪問使用jQuery的方法的一種方式。

回答

0

你從jquery中獲得什麼好處?在jQuery中直接使用JS是完全正確的。 document.getElementById('plugin').updateDisplay('blah', 'bleh')。但如果你真的想,你可以創建一個插件。

jquery.fn.updateDisplay = function(a, b) { 
    this.each(function(index, el){ 
    el.updateDisplay(a, b); 
    }); 
    return this; 
} 

//... and so on 

下面是做這件事的通用方法:

function convertMethodsTojQuery(/* methodName1, methodName2, ...*/) { 

    // In some browsers (*cough* IE *cough*), the methods of DOM 
    // objects are not real JavaScript Functions and don't 
    // support the apply method. Borrow Function's apply. 
    var apply = Function.prototype.apply; 

    for (var i=0; i < arguments.length; i++) { 
    var methodName = arguments[i]; 
    jQuery.fn[methodName] = (function(name) { 
     return function() { 
     // Save arguments so it's available in the inner looping closure 
     var args = arguments; 
     this.each(function(index, el){ 
      apply.call(el[name], el, args); 
     }); 
     }   
    })(name); 
    } 
} 

// Call it like 
convertMethodsTojQuery("updateDisplay", "printReceipt", "openDrawer") 

用的一般方法是相當地參與(借款Function.prototype的和自我調用函數的循環變量分離),所以它不容易理解除非你對JavaScript有很好的把握