'+ variable +'是什么意思? [英] What does '+variable+' mean?

查看:89
本文介绍了'+ variable +'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么:'+ variable +'是什么意思?

What does this: '+variable+' mean?

我使用jQuery append方法添加一些DOM元素:

I use jQuery append method to add some DOM element:

$('#story_pages').append('<div><button value="'+window_value+'" onclick= "reload_to_canvas(this.value)" ><img id="w'+window_value+'", src=white_data_URL, width="100px", height="100px"/> </button></div>');

实际上,我不知道为什么这样做...
如果我只是写

Actually, I don't why this works... If I just write

value= window_value 
img id= w+window_value

,并使用选择器
$('#(w + window_value)')
查找元素。它必须失败。


这个运算符是什么? '+ variable +'


我们为什么要使用奇怪的符号'+ variable +'?


我们什么时候应该使用这种表示法?

,and use the selector $('#(w+window_value)') to find the element. It must fail.
What is this operator? '+variable+'
And why should we use the weird notation "'+variable+'"?
When should we use this notation?

推荐答案


这个运营商是什么? '+ variable +'

What is this operator? '+variable+'

这不是运算符。

'结束字符串文字

+ 是串联运算符。

变量是一个字符串变量

+ 是另一个连接运算符。

+ is another concatenation operator.

'启动一个新的字符串文字。

' starts a new string literal.


我们为什么要使用奇怪的符号'+ variable +'

这两个字符串文字的数据中包含个字符。

The two string literals have " characters in their data.

对象是构造这个:

'<element attribute="value">'

当值是变量时

var myValue = "value";
'<element attribute="' + value + '">'

通过字符串连接从代码生成代码总是会提供难看的代码,这是相对难以维护的。我会用更详细的方法解决问题:

Generating code from code by string concatenation always gives ugly code, which is relatively hard to maintain. I'd approach the problem with a more verbose approach:

var content = $("<div>");
var button = $("<button>");
button.val(window_value);
button.on('click', function() { reload_to_canvas(this.value); });
var img = $('<img>');
img.attr('id', 'w' + window_value);
img.attr('src', 'white_data_URL');
// width and height can be handled in CSS
button.append(img)
content.append(button)
$('#story_pages').append(content)

这篇关于'+ variable +'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆