1
<script type="text/javascript">
    function achat_change_themes() {
        var rel = $('link[title="chat_theme"]');
        var select = $('#achat_times').attr('value');
  if(select == "GrayScale") {
       rel.attr('src' , 'www.avacwebthemes.vacau.com/css/mario_theme.css');
    }
  else if (select == "Mario") {
     rel.attr('src' , 'www.avacwebthemes.vacau.com/css/mario_theme.css');
    }
  else if (select == "Eartone") {
     rel.attr('src' , 'www.avacwebthemes.vacau.com/css/mario_theme.css');
     }
  else if (select == "Dark") {
     rel.attr('src' , 'www.avacwebthemes.vacau.com/css/mario_theme.css');
     }
  else if (select == "Floral") {
     rel.attr('src' , 'www.avacwebthemes.vacau.com/css/mario_theme.css');
     }
  else if (select == "Military") {
     alert(www.avacwebthemes.vacau.com/css/mario_theme.css);
     }

  }
 </script>

HTML 模拟

<select id="achat_themes" onChange="achat_change_themes();">
  <option value="GrayScale">GrayScale</option>
    <option value="Mario">Mario</option>
      <option value="EarthTone">EarthTone</option>
        <option value="Dark">Dark</option>
          <option value="Floral">Floral</option>
            <option value="Military">Military</option>
  </select>

基本上我想要做的是在 select 的 change 事件上它会改变 head 中链接标签的 src ......例如

<link type="text/css" rel="stylesheet" title="chat_theme" href="http://sourcefile.com/css/file.css">

并且希望每次我在测试这个时提醒最后一个选择时更改它,只需 5 分钟的时间,并尝试使用 jQuery 的 javascript 函数。它没有运行任何建议?

我尝试取出整个内部代码,但我不断得到未定义的 achat_change_themes?我一定是写错了函数?

4

3 回答 3

1

I have a few suggestions, both trying to answer your questions directly and, if you don't mind, also trying to help improve your code:

Suggestion 1: I think you're looking for the 'href' property of the link, not 'src'.

Suggestion 2: I'm guessing it's just to make the point, but just in case (I've forgotten many things like this): make sure to change 'www.avacwebthemes.vacau.com/css/mario_theme.css' to the appropriate address when the time comes.

Suggestion 3: Use the javascript switch statement in your function instead of a bunch of if's. It's way cleaner:

switch(select)  
{  
    case "Grayscale":     
    {
        rel.attr('href', 'www.avacwebthemes.vacau.com/css/mario_theme.css');  
        break;
    }

    case "Mario":  
    {
        rel.attr('href', 'www.avacwebthemes.vacau.com/css/mario_theme.css');  
        break;
    }
    //Etc
}

Suggestion 4: If you are trying to get into jQuery, I'd recommend using something like the following code (the script block goes inside the body, not the head):

<script>
$(function() {
        $('#achat_themes').change( function() {
                var rel = $('link[title="chat_theme"]');
                switch($(this).val())
                {  
                    case "Grayscale":     
                    {
                        rel.attr('href', 'www.avacwebthemes.vacau.com/css/mario_theme.css');  
                        break;
                    }

                    case "Mario":  
                    {
                        rel.attr('href', 'www.avacwebthemes.vacau.com/css/mario_theme.css');  
                        break;
                    }
                    //Etc
                }
            });
    });
</script>
于 2013-02-03T07:46:34.607 回答
0

使用 jQuery change() 事件而不是 onChange:

attr("src",link);错了-您应该改为更改 href 。

而不是使用attr("val")来获取下拉列表的值,您需要 val()。

你也拼错#achat_themes#achat_times

Here's a fiddle with all of your problems fixed:

http://jsfiddle.net/498Z5/

于 2013-02-03T07:02:10.673 回答
0

Why don't you try this way:

Try the fiddle out: http://jsfiddle.net/Ng24P/

$(function () {
    $('#achat_themes').change(function () {
        changeTheme($(':selected').val());
    });
});

function changeTheme(theme) {
    var stylshit = $('[title="chat_theme"]');
        stylshit.attr('href','http://sourcefile.com/css/'+theme.toLowerCase()+'.css');
        alert(stylshit.attr('href'));
}
于 2013-02-03T09:20:14.747 回答