简体   繁体   English

javascript-如何从函数中获取所选元素并将其作为另一个函数的参数传递

[英]javascript - how to take selected element from function and pass it through as a parameter of another function

I have been trying for days upon weeks with trying to build a personal library without jQuery for my school club, and so far I am hitting a rut when it comes to passing through an element or objects through to another function. 我已经尝试了好几天,试图为我的学校俱乐部建立一个不带jQuery的个人库,到目前为止,当涉及到通过一个元素或对象传递到另一个函数时,我一直很沮丧。 The notation I am trying for is this : 我正在尝试的符号是这样的:

CC(function(){
    CC('id:wrapper').set('html','Hello World!');
});

That is my test code, and the library looks as it does below: 那是我的测试代码,该库如下所示:

    "use strict";

    var CC = function () {
        var args = arguments[0] || {};
        if(typeof args === "object") {
            args = args || {};
        }
        else if(typeof args === "function") {
        args = arguments[0];
        return window.onload = args;
    }
    else if(typeof args !== "object" || typeof args !== "function") {
        var elem = get(args);
        return elem;
    }
};

CC({
    //Can only be done once. Will return TypeError because '$' won't exist afterward
    noConflict : function (name) {
        name = new CC();
        return name;
    }
});
//The way to modify things
CC.mod = CC.prototype = {};
CC.extend = CC.mod.extend = function () {
    var args = arguments[0] || {};
    var target = get(args);
    return target;
};
CC.mod.extend({
    //Use psuedo types to set specific values (required)
    set : function(type, value) {
        return set(this.target, type, value);
    }
});
//General get function to get selectors, generate functions, or return values
function get() {
    var args = arguments[0] || {};
    //Check if the argument is a function
    //If it is, return the function on page load
    if (typeof args === "function") {
        return window.onload = args;
    }
    //Check argument type
    if(typeof args !== "object") {
        args = arguments[0];
        return args;
    }
    else {
        args = {};
        return args;
    }
    //Check if args has an elem psuedo
    if(args.indexOf("id:") > -1 || args.indexOf("class:") > -1 || args.indexOf("tag:") > -1) {
        var target = args;
        //Run id psuedo
        if(target.indexOf("id:") > -1) {
            target = target.replace('id:','');
            console.log(target);
            return document.getElementById(target);
        }
        //Run class psuedo
        else if(target.indexOf("class:") > -1) {
            target = target.replace('class:','');
            console.log(target);
            return document.getElementsByClassName(target);
        }
        //Run tag psuedo
        else if(target.indexOf("tag:") > -1) {
            target = target.replace('class:','');
            console.log(target);
            return document.getElementsByTagName(target);
        }
    }
    //Check if args is not null
    //If not, then return args value
    if(args !== null) {
        return args.value;
    }
    else {
        return null;
    }
}
//General function to set things for elements
function set(elem, property, value) {
    //If the element provided is part of getting an element
    //If it is, run the psuedo checker
    if(elem.indexOf("id:") > -1 || elem.indexOf("class:") > -1 || elem.indexOf("tag:") > -1) {
        elem = get(elem);
        //Rerun the set() function to set properties and values
        set(elem, property, value);
    }
    //If not, then run the type psuedo checker
    else {
        //Check if style
        if(property.indexOf("css:") > -1 || property.indexOf("style:") > -1) {
            //Check for the independent types
            if(property.indexOf("css:") > -1) {
                property = property.replace('css:','');
                return elem.style[property] = value;
            }
            else if(property.indexOf("style:") > -1) {
                property = property.replace('style:','');
                return elem.style[property] = value;
            }
        }
        //Check if attribute
        else if(property.indexOf("attr:") > -1) {
            property = property.replace('attr:','');
            return elem.setAttribute(property, value);
        }
        //Check if html
        else if(property.indexOf("html") > -1) {
            return elem.innerHTML = value;
        }
        //To add more, just add another else if(condition...) {Code} statement
        //Condition must be defined in psuedo selectors
        //Condition must be property.indexOf("selector:" > -1)
        //return statement must consist of a return value from the value     parameter
        }
    }

I don't know how to get my methods to pass through correctly and I don't know how to get my methods to apply to the element in the CC('id:wrapper') code. 我不知道如何正确地传递我的方法,也不知道如何将我的方法应用于CC('id:wrapper')代码中的元素。 I already have the 'psuedo selector' made to get rid of the id: code. 我已经做了“伪选择器”来摆脱id:代码。 Any help would be much appreciated! 任何帮助将非常感激!

You've posted quite some code which I wasn't able to get to work quickly, so I'm not sure if this will help you out. 您已经发布了很多我无法快速启动的代码,因此我不确定这是否对您有帮助。

The basic idea is that your CC method will always have to return an object with a set method. 基本思想是, CC方法将始终必须使用set方法返回对象。 If there's no element with id="wrapper" , you'll have to figure out a way to handle exceptions. 如果没有id="wrapper"元素,则必须找出一种处理异常的方法。

You can use bind to create a new function from an earlier defined function with a pre-bound this context and pre-filled in arguments. 您可以使用bind创建一个新的功能,从早期定义的函数与预先绑定this上下文和预填充的参数。

A simplified example: 一个简化的例子:

 var CC = function(query) { return { set: set.bind(null, document.querySelector(query)) }; } function set(element, attr, val) { element.setAttribute(attr, val); } CC("input").set("placeholder", "I was set by js"); 
 <input type="text" /> 

If you want to do more advanced binding of arguments, I'd suggest you google "Currying". 如果您想对参数进行更高级的绑定,建议您使用谷歌“ Currying”。 With some code, you can make functions automatically return new functions when called with less arguments than needed. 使用某些代码,可以使函数在调用时使用比所需数量更少的参数时自动返回新函数。

What .bind does: .bind是:

The bind method is defined in Function.prototype . bind方法在Function.prototype定义。 You can call it on any function you've defined to create a new function. 您可以在定义的任何函数上调用它来创建函数。

The first argument that goes in to bind, is used as the this context in the newly created function. 进入绑定的第一个参数在新创建的函数中用作this上下文。 You could, for example, do: 例如,您可以执行以下操作:

 var myDiv = document.querySelector("div"); var logText = function() { console.log(this.innerText); }; var logDivText = logText.bind(myDiv); logText(); // Bound to window, logs undefined logDivText(); // Bound to div, logs text 
 <div>Text in a div</div> 

Any other arguments passed to bind, are automatically passed as arguments. 传递给绑定的任何其他参数将自动作为参数传递。 For example: 例如:

 var sum = function(a, b) { return a + b; }; var sum3 = sum.bind(null, 3); // we don't use this, so we don't define it console.log(sum3(5)); // Prints 8 

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

相关问题 如何从html调用javascript函数并通过它传递参数 - how to call javascript function from html and pass parameter through it 如何通过 886982359588 参数传递数组 - javascript - How to Pass An Array Through a Function Parameter - javascript javascript将元素从一个函数传递到另一个函数 - javascript pass element from one function to another 如何删除或清空通过 function 参数选择的 HTML 元素? - How to delete or empty an HTML element selected through function parameter? 如何在从另一个JavaScript函数调用的JavaScript函数中传递多个参数? - How to pass multiple parameter in javascript function which is called from another javascript function? Javascript:如何将带有字符串参数的函数作为参数传递给另一个函数 - Javascript: How to pass a function with string parameters as a parameter to another function 将带有参数的 function 作为参数传递给 JavaScript 中的另一个 function - Pass a function with parameter as an argument to another function in JavaScript 如何将参数从javascript传递到Ajax函数 - How to pass parameter from javascript to Ajax function 如何从Javascript函数将参数传递给XSLT - How to pass parameter to XSLT from Javascript function 我如何通过嵌套函数将元素作为参数传递? - how would i pass the element as a parameter through the nested function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM