【问题标题】:jquery attr not working in chrome?jquery attr 在 chrome 中不起作用?
【发布时间】:2011-02-18 18:07:39
【问题描述】:
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)

【问题讨论】:

    标签: jquery google-chrome attr


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 非常感谢!既然您对 jQuery 非常了解,您介意告诉我如何缩短我的其余代码吗?
    【解决方案2】:
    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-08-28
      • 1970-01-01
      • 2016-11-02
      • 2012-09-21
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多