jquery attr 在 Chrome 中不起作用?

发布于 2024-10-18 05:48:09 字数 1128 浏览 4 评论 0原文

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

This works in FF but not Chrome. What happens in Chrome is that the class is set, but the createDisicipline() isn't.

Is this the wrong way to add multiple attrs to a tag? It does work in FF :/

ENTIRE CODE:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-10-25 05:48:10

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

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

注意我的变化。我将 class 括在引号中,因为它是 InternetExplorer 中的关键字,否则可能会破坏您的代码。我还将新创建的

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

You should definitely not try to bind an event handler like this. Do it the unobtrusive way:

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

Notice my changes. I wrapped class in quotes because it is a keyword in InternetExplorer and may break your code otherwise. Also I appended the newly created <select> node to the document.body.

Demo: http://jsfiddle.net/vM5Hp/

微凉 2024-10-25 05:48:10
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
    });
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
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文