将方法应用于变量

Dav*_*cia 2 javascript jquery

我试图将一个方法添加到一个变量,用于以下;

var method = ".wrap";   
jQuery('.container')+[method]+("<div>Hello World</div>");
Run Code Online (Sandbox Code Playgroud)

基本上它应该做的是;

jQuery('.container').wrap("<div>Hello World</div>");
Run Code Online (Sandbox Code Playgroud)

但它不起作用,没有错误,页面中没有添加hello world div.这是原始问题http://goo.gl/MQ2tr,我想我会用更简单的术语再次提问.

更新 只要我在变量中有一个单独的方法,它与括号表示法很好用,但是我需要使用类似的东西,我.parent().parent().wrap()怎样才能使它工作?我尝试删除所有的点,但我只是得到一个错误Uncaught TypeError: Object [object Object] has no method 'parent()parent()wrap'

这是我的列表表单现在的样子

 <select name="lu_ban_data[method]" id="lu_ban_data" />
        <option value="append">Append</option>
        <option value="prepend">Prepend</option>
        <option value="wrap">Wrap</option>
        <option value="parent()parent()wrap">Parent</option>
</select>
Run Code Online (Sandbox Code Playgroud)

t.n*_*ese 5

只需这样写:

var method = "wrap";   
jQuery('.container')[method]("<div>Hello World</div>");
Run Code Online (Sandbox Code Playgroud)

您可以通过两种方式访问​​对象的属性.通过点表示法方括号表示法

 obj.property
 obj['property']

 var propName = "property"
 obj[propName]
Run Code Online (Sandbox Code Playgroud)

编辑

这里是MDN 会员运营商的链接

对代码执行操作的简短说明:

  jQuery('.container')+[method]+("<div>Hello World</div>");
Run Code Online (Sandbox Code Playgroud)

它是3个元素的补充:

  1. 结果集 jQuery('.container')
  2. 一个Array包含一个元素[method]
  3. String "<div>Hello World</div>"

结果取决于实现,但很可能是这样的:

"[object Object].wrap<div>Hello World</div>"
 +-------------+
                +---+
                     +--------------------+
Run Code Online (Sandbox Code Playgroud)

结果看起来是这样的,因为JavaScript-Engines通常会调用toString元素,如果它们不能以其他方式添加它们.

编辑

更新已编辑的问题:

 element.parent().parent().wrap()
Run Code Online (Sandbox Code Playgroud)

例如等于:

 element['parent']()['parent']().wrap()
Run Code Online (Sandbox Code Playgroud)

要么

 element['parent']().parent()['wrap']()
Run Code Online (Sandbox Code Playgroud)

或任何其他组合ob 括号表示法

您希望表示.parent().parent().wrap()为一个字符串并将其用作访问权限.但这不会那样.该点符号括号符号只返回给定元素的属性.因此,在您调用的返回对象和您调用的返回对象上parent()返回parentofjQuery('.container')parant()wrap()

所以(假设只有你的最后一个函数调用会有参数)你需要这样的东西:

function chainedFunctionCall( obj, chain, arguments) {
    var curr = obj;
    var splitChain = chain.split(".");  //split the 'call chain' passed a strings by '.' (the dot in the string has nothing to do with the dot notation)

    //iterate over the resulting array (except the last one where we need to pass the arguments to
    for( var i=0 ; i<splitChain.length-1 ; i++ ) {
        //call the function by the given name in the chain and store the result as current object
        curr = curr[splitChain[i]]();
    }

    //when we reached the last name in the chain call that function using `apply` so that we can pass the arguments we got as array to the function call, and call it in the context of the current object.
    return curr[splitChain[i]].apply(curr,arguments);
}


var obj = $(".container");
var callChain = "parent.parent.wrap";


chainedFunctionCall( obj, callChain, ["<div>your argument you pass there</div>"]);
Run Code Online (Sandbox Code Playgroud)