简体   繁体   English

JavaScript只能运行一次

[英]JavaScript only works once

I have a javascript that seems to only run once. 我有一个似乎只运行一次的JavaScript。 I am using this to select the colour of a letter inside a div. 我用这个来选择div里面一个字母的颜色。 When I select the colour initially it works but then will not select a different colour. 当我最初选择颜色时,它可以工作,但不会选择不同的颜色。

This is the select: 这是选择:

 <div style = "position: absolute; left: 360px; top: 590px;" >    
 <select id="selectcolor1" name="selectcolor1" style="width: 150px;">
                    <option value="null">Select a Motif Color...</option>
                    <option value="black">Black</option>
                    <option value="blue">Blue</option>
                    <option value="darkblue">Dark Blue</option>
                    <option value="pink">Pink</option>
                    <option value="green">Green</option>
                    <option value="orange">Orange</option>
                    <option value="seagreen">Sea Green</option>
                    <option value="red">Red</option>
                    <option value="darkgreen">Dark Green</option>
                    <option value="bergundy">Bergundy</option>
                    <option value="cyan">Cyan</option>
                    <option value="magenta">Magenta</option>
                    <option value="mustard">mustard</option>
                    <option value="purple">Purple</option>

This is the javascript: 这是javascript:

 $('#selectcolor1').change(function() {
var color = this.value || '';
$('.selectcolor1').attr('class', function(i, classes) {
    var cls = classes.split(/[\s]/);
    cls[2] = color;
    return cls.join(' ');
});
 e.preventDefault();
});  

I probably should have mentioned I am using the script to change to different Div's colours apologies my fault: 我可能应该提到我正在使用脚本更改不同Div的颜色表示歉意,这是我的错:

      <div style = "position: absolute; left: 750px; top: 313px;" 
class="selected-value  selectcolor1" id ="motif" ></div>


<div style = "position: absolute; left: 750px; top: 313px;" 
class="selected-value1 selectcolor1" id ="motif1" ></div>

Any help would be great thank you! 任何帮助都会非常感谢你!

Couple of problems here. 这里有几个问题。

As @cport noted, you are not declaring the e variable you are referring to in e.preventDefault() . 正如@cport所指出的,您没有在e.preventDefault()声明所引用的e变量。 This needs to come in the change function handler. 这需要进入change函数处理程序。

As @dsuess noted, your code is changing the class on a tag with class="selectcolor1", which isn't in your pasted code - please make sure this is correct. 正如@dsuess所说,你的代码正在改变标签上的类,其中class =“selectcolor1”,这不在你的粘贴代码中 - 请确保这是正确的。

Also, this code will work with the jsfiddle provided below since it sets the target's third class and the target has two classes initially. 另外,此代码将与下面提供的jsfiddle一起使用因为它设置了目标的第三类,并且目标最初具有两个类。 However, if you change the target HTML to have three or more classes from the start, the Javascript code will overwrite the third class. 但是,如果您将目标HTML从一开始就更改为三个或更多类,则Javascript代码将覆盖第三个类。

But having said that, this works with the current code: 但话说回来,这适用于当前的代码:

$('#selectcolor1').change(function(e) {
    var color = this.value || '';
    $('.selectcolor1').attr('class', function(i, classes) {
        var cls = classes.split(/[\s]/);
        cls[2] = color; // third class becomes the color value
        return cls.join(' ');
    });
    e.preventDefault();
});

As @cport1 stated, the anonymous function is missing the e parameter: 如@ cport1所述,匿名函数缺少e参数:

$('#selectcolor1').change(function(e) { ...

This will cause the JS to be fired only once and then error because there is no reference to e in that function's scope. 这将导致JS只被触发一次然后出错,因为在该函数的范围内没有对e引用。

You are missing the e parameter in your event binding: 您在事件绑定中缺少e参数:

 $('#selectcolor1').change(function()

replace with 用。。。来代替

 $('#selectcolor1').change(function(e)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM