句法?单击元素时将字符串变量设置为字符串值
$('#Colors span').live('click', function(){
var layerFind = "BACK";
if ($(this).hasClass('.CI_W')) {
layerFind = "WHITE";
} else if ($(this).hasClass('.CI_U')) {
layerFind = "BLUE";
} else {
layerFind = "BASE";
}
$("#Base").attr("src", 'Layers/' + layerFind + '.png');
});
我有代表颜色的图标的跨度。当用户单击该图标时,字符串变量 layerFind 会更改为适当的颜色。然后将该字符串用作图像文件的 src。目前它适用于 BASE 字符串值,这是我加载页面时的默认值,但它不响应跨度单击。
我想知道 switch 语句是否更有意义。稍后我将需要这个脚本来组合单击的颜色。例如,如果我单击白色图标和黑色图标,则字符串变为 WB,缩写,并且具有该名称的图像文件是灰色图像。如果我单击 3 个或更多,那么它会使用辅助默认值,例如 RAINBOW 等。
$('#Colors span').live('click', function(){
var layerFind = "BACK";
if ($(this).hasClass('.CI_W')) {
layerFind = "WHITE";
} else if ($(this).hasClass('.CI_U')) {
layerFind = "BLUE";
} else {
layerFind = "BASE";
}
$("#Base").attr("src", 'Layers/' + layerFind + '.png');
});
I have spans that are icons that represent colors. When a user clicks the icon, the string variable layerFind changes to the appropriate color. This string is then used as the src for an image file. Currently it works for the BASE string value, that is the default when i load the page, but it doesn't respond to the span clicks.
I'm wondering if a switch statement makes more sense. Later on i will need this script to be able to combine the colors clicked. For example, if i click the white icon and the black icon, the string becomes WB, an abbreviation, and the image file with that name is a gray image. If i click 3 or more, then it uses a secondary default, for example, RAINBOW or something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老兄,你的问题很简单,只需删除 ifs 内变量中的 de $ 即可
dude, your trouble is simple, just remove de $ in the variables inside the ifs