jquery attr不在chrome中工作?

时间:2011-02-18 18:07:40

标签: jquery google-chrome attr

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

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

这是向标签添加多个attrs的错误方法吗?它在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)

2 个答案:

答案 0 :(得分:4)

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

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

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

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

答案 1 :(得分: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
    });