【问题标题】:Created jQuery Elements are not in jQuery Style创建的 jQuery 元素不是 jQuery 样式
【发布时间】:2012-11-07 08:27:24
【问题描述】:

我是所有 JavaScript 和 jQuery-Stuff 的新手。请原谅我的编码或肮脏风格的任何愚蠢错误。我很高兴收到您经验丰富的所有建设性反馈。

我尝试动态添加一些 jQuery 元素,比如一些新的选择菜单。在我的示例中,用户将决定他想要定制多少辆汽车,并在此基础上为每辆选择的汽车创建一个新的选择菜单。

如果用户选择“2 辆汽车”,jQuery 将添加两个新的选择菜单。但我的问题是这些菜单不再是 jQuery 样式了。

我在这里做错了什么?是否也可以根据选择的汽车数量在“for-loop”中添加新菜单?

这是我的代码:

<script type="text/javascript">

    $('#select-choice-1').live( "change", function(event, ui) {
        var valuee = $(this).val();

        if (valuee == '2') ($('<label for="select-choice-2" class="select">Color of car #1</label>' +
                                          '<select name="select-choice-2" id="select-choice-2">'+
                                          '<option value="red">red</option>'+
                                          '<option value="blue">blue</option>'+
                                          '</select><br><br>' +
                              '<label for="select-choice-3" class="select">Color of car #2</label>' +
                                          '<select name="select-choice-3" id="select-choice-3">'+
                                          '<option value="green">green</option>'+
                                          '<option value="yellow">yellow</option>'+
                                          '</select>').appendTo('#site'));
        if (valuee == '3') ($('<h1>Test</h1>').appendTo('#site'));

    });
    </script>

这里是html页面:

<div id="site" data-role="fieldcontain">    

    <label for="select-choice-1" class="select">Number of cars</label>
    <select name="select-choice-1" id="select-choice-1">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>


    </div><!-- "site" -->

【问题讨论】:

  • “jQuery 风格”是什么意思?
  • 它看起来就像普通的 html 样式。没有阴影,只有这样的老式选择字段:vaannila.com/images/struts1/HtmlSelectTag1Pic1.gif
  • 如果 jQuery 样式是指 jQuery-ui 样式,那么您需要在 html 中添加与 jQuery ui 相同的类。
  • 你想怎么做这个?
  • 如果你想拥有自己的样式,然后添加 css 并将这些类添加到这些元素中

标签: jquery styles


【解决方案1】:
        $('#site').delegate("#select-choice-1", "change", function(event, ui) {
           var valuee = $(this).val();
           var constructhtml = '';
             if (valuee == '2') {
               constructhtml +='<label for="select-choice-2" class="select">Color of car #1</label>' +   '<select name="select-choice-2" id="select-choice-2">'+ '<option value="red">red</option>'+ '<option value="blue">blue</option>'+ '</select><br><br>' + '<label for="select-choice-3" class="select">Color of car #2</label>' + '<select name="select-choice-3" id="select-choice-3">'+ '<option value="green">green</option>'+ '<option value="yellow">yellow</option>'+ '</select>';
             }else if (valuee == '3') {
               constructhtml +='<h1>Test</h1>';
             }
    $('#site').append(constructhtml);
     });

OP 需要创建一个动态添加的选择菜单

将此 div 放在页面的最底部

<div id="selectcontainer"></div>

$(document).ready(function(){

var str = '';
str+='<select>';
   for(i=1;i<=10;i++){
    str+='<option value='"+i+"'>'"+i+"'</option>';

  }   
str+='</select>';
 $('#selectcontainer').html(str);
});

【讨论】:

  • 可能是设计的一些 CSS 问题..我添加了一些调整(删除了 live 并添加了格式)
  • 我只是拿了原来的 jquery.css 并没有改变任何东西。我不想要任何新样式,我只想让新的选择菜单看起来像一个普通的 jquery 选择菜单
  • 如果您使用 firebug 和 Mozilla..分析选择菜单并查看适用的样式
  • firebug 没有在该测试页上打开
  • 确定,然后删除该选择菜单的所有类以及与选择相关的 jquery.css 注释样式 ..并尝试
猜你喜欢
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 2011-01-04
  • 2012-03-24
  • 2011-11-24
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多