简体   繁体   English

如何将原型 jquery function 转换为 javascript 原型 ZC1C425268E68385D1AB5074C141

[英]How to convert a prototype jquery function to javascript prototype function

I am using a crossword in jquery and for some reason i need to convert it to javascript and avoid using jquery.我在 jquery 中使用填字游戏,出于某种原因,我需要将其转换为 javascript 并避免使用 jquery。 I have managed to convert many parts of it but i have a big problem in a prototype function and i cant understand how to convert it.我已经设法转换了它的许多部分,但我在原型 function 中有一个大问题,我无法理解如何转换它。

Here is the code in summary:以下是代码摘要:

$("#"+cw_id).crossword();

(function($) {
// plugin definition
$.fn.crossword = function(options) {
    .....
    // implementation code goes here.    
    return this.each(function(){
      ....
    }
})();
// end plugin definition

I have tried doing this and similar to this calls but nothing is working fine.我已经尝试过这样做并且与此调用类似,但没有任何工作正常。

document.getElementById("#"+cw_id).crossword();
(function() {
// plugin definition
Object.prototype.crossword = function(options) {

})();
// end plugin definition

The actual code of this prototype function is at this link: https://github.com/electricg/jQuery-Html5-Crossword/blob/master/crossword.js#L1这个原型function的实际代码在这个链接: https://github.com/electricg/jQuery-Html5-Crossword/blob/master/crossword.js#L1

and the caller is at the index link: https://github.com/electricg/jQuery-Html5-Crossword/blob/master/index.html#L62调用者位于索引链接: https://github.com/electricg/jQuery-Html5-Crossword/blob/master/index.html#L62

I really need to know how to convert this prototype function back to javascript Thank you in advance for any help provided我真的需要知道如何将此原型 function 转换回 javascript 提前感谢您提供的任何帮助

For similar functionality to jQuery, you're looking for HTMLElement.prototype .对于与 jQuery 类似的功能,您正在寻找HTMLElement.prototype

 HTMLElement.prototype.crossword = function(text) { this.innerHTML = text; }; document.getElementById('crossword1').crossword('crossword here');
 <div id=crossword1 />

Since that's a bad idea , I would use a function and bind the element to get a similar result. 由于这是一个坏主意,我将使用 function 并绑定元素以获得类似的结果。 You can also pass it in as an argument, but that would require more refactoring of the existing code.您也可以将其作为参数传递,但这需要对现有代码进行更多重构。

 const crossword = function(text) { this.innerHTML = text; }; crossword.bind(document.getElementById('crossword1'))('crossword here');
 <div id=crossword1 />

Here you're calling function on HTML element在这里,您在 HTML 元素上调用 function

document.getElementById("#"+cw_id).crossword();

hence you need to define the function on it's prototype因此您需要在其原型上定义 function

HTMLElement.prototype.crossword = function(options) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM