0

很简单,但我做错了什么。我想在这里做一个漂亮的干净链,而不是声明一个 var 然后连接它:

the_function( null, null, 
   $( this ) // START string I'm passing as a parameter
   .attr( 'data-href' )
   .html( '|foo:' + event.var ) 
); // END string I'm passing as a parameter

我意识到.html()这里是无效的,什么是正确的?

4

3 回答 3

3

嗯?你的意思是做这样的事情吗

the_function( null, null, 
   $( this ) // START string I'm passing as a parameter
   .attr( 'data-href' ) +  '|foo:' + event.var); 

你有什么

$( this ).attr( 'data-href' ) // returns a string.. it doesn't have a html method
于 2012-10-25T19:19:11.273 回答
0

我不太确定你想要完成什么,但这里有一些注意事项:

  • 您可以将 .data() 方法用于数据属性
  • 你不能在 .attr() 上使用 .html(),你需要在 $(selector) 上使用 html()

所以,基本上,你可以使用这个:

$(this).html('|foo:' + event.var).data('href')

于 2012-10-25T19:20:20.520 回答
0

我不确定你想要什么结果,但如果你真的想要方法链接,你可以将本机.concat()方法链接到字符串。

the_function( null, null, 
   $( this ).attr( 'data-href' )
            .concat('|foo:', event.var) 
);

.concat()或者,如果您真的挖掘链接,则单独调用。

the_function( null, null, 
   $( this ).attr( 'data-href' )
            .concat('|foo:')
            .concat(event.var) 
);
于 2012-10-25T19:20:22.677 回答