简体   繁体   English

如何根据上的函数更改javascript css图像 <select>元件?

[英]How to change a javascript css image based on a function on the <select> element?

I have this select object: 我有这个选择对象:

<select id="alunos" onchange="arteAluno(this);">
    <option selected="selected"></option>
    <option value="loadImage();">Aluno 1</option>
    <option value="../Index.html">Aluno 2</option>  
    <option value="../Index.html">Aluno 3</option>
</select>

and this function: 而这个功能:

function arteAluno (sel) {
    var url = sel[sel.selectedIndex].value;
    window.location = url;
}

and I was making the page change with this. 我正在用这个改变页面。 But now I want to get this code: 但现在我想得到这个代码:

$(document).ready(function() {

    function O(obj) {
        return document.getElementById(obj);
    }
    function loadImage() {
        O('arte').style.backgroundImage="url(aikido.jpg)";
        O('arte').style.width = '200px';
        O('arte').style.height = '160px';
    }
})

and make this function loadImage only be triggered when I select one of the options of the tag. 并且只有当我选择标签的一个选项时才会触发此函数loadImage。

You need to intercept the selectedIndex (or the value) and call your function in your own code: 您需要拦截selectedIndex(或值)并在您自己的代码中调用您的函数:

function arteAluno (sel) {
    var url = sel[sel.selectedIndex].value;
    if(url=='loadImage();') {
        loadImage();
        return;
    } 
    window.location = url;
}

Since you are already using jQuery, you might just as well use it for everything here. 由于您已经在使用jQuery,因此您也可以将它用于此处的所有内容。 You only need to bind one .change() handler which sets window.location if one of the URLs is chosen, and executes the loadImage() function otherwise. 您只需要绑定一个.change()处理程序,如果选择了其中一个URL,则设置window.location ,否则执行loadImage()函数。

Rather than call the function name loadImage() in the <option> value (which looks strange), I have replaced it below with a special value value='load_image' . 而不是在<option>值(看起来很奇怪)中调用函数名称loadImage() ,而是在下面用特殊值value='load_image'替换它。 The .change() handler will look for that value and execute the function if found. .change()处理程序将查找该值并在找到时执行该函数。

<select id="alunos">
   <option selected="selected"></option>
   <!-- Special value rather than function name here -->
   <option value="load_image">Aluno 1</option>
   <!-- URL values -->
   <option value="../Index.html">Aluno 2</option>  
   <option value="../Index.html">Aluno 3</option>
</select>

And the jQuery: 和jQuery:

$(document).ready(function() {
  // Bind the onchange event for select#alunos
  $('#alunos').change(function() {
    // If the load_image option was selected, call the loadImage() function
    if ($(this).val() == 'load_image') {
      loadImage();
    }
    // Otherwise, load the URL
    else {
     // Unless the blank option is selected...
     if ($(this).val() !== '') {
       window.location = $(this).val();
     }
    }
  });
});
// Can replace with all jQuery (since you are using it anyway)
function loadImage(){
  $('#arte').css('backgroundImage', 'url(aikido.jpg)');
  $('#arte').css('width', '200px');
  $('#arte').css('height', '160px');
}

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

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