2
var select_tag = $('<select></select>').attr( {
    onchange: 'createDiscipline()',
    class: 'select_menu'}
).fadeIn('slow');

这适用于 FF 但不适用于 Chrome。在 Chrome 中发生的情况是类已设置,但 createDisicipline() 未设置。

这是将多个属性添加到标签的错误方法吗?它确实在FF中工作:/

整个代码:

var select_tag = $('<select>', {
                    onchange: createDiscipline,
                    'class': 'select_menu'}
                ).fadeIn('slow');
            for(i = 0; i < c_data.length; i++) {
                select_tag.append('<option>' + c_data[i] + '</option>');
            }

            //wrap select_tag in div
            var div_buffer = $('<div class=\'select_buffer\'></div>');
            var div_container = $('<div class=\'select\'></div>');
            div_buffer.append(select_tag).wrap('<div class=\'select_buffer\' />');
            div_container.append(heading);
            div_container.append(div_buffer);

            //Append to the page after right_content id
            $('#right_content').append(div_container)
4

2 回答 2

4

您绝对不应该尝试像这样绑定事件处理程序。以不引人注目的方式进行:

var select_tag = $('<select>', {
    change:  createDiscipline,
    'class': 'select_menu'
}).appendTo(document.body).fadeIn('slow');

注意我的变化。我用class引号括起来,因为它是 InternetExplorer 中的关键字,否则可能会破坏您的代码。我还将新创建<select>的节点附加到 document.body 中。

演示:http: //jsfiddle.net/vM5Hp/

于 2011-02-18T18:11:15.080 回答
0
Some time it’s very frustrated that jQuery .attr not working with Chrome.
    Here I am giving you solution for this.
    Try:
    $(window).load(function () {
    //your script or function
    });
    Rather then
    $(document).ready(function () {
    //your script or function
    });
于 2011-11-01T21:26:39.973 回答