dwa4821 2016-06-17 06:25 采纳率: 71.4%
浏览 2433
已采纳

jquery获取元素 元素的id是变量

点击按钮后 。只生成了div ,span没有

        $("button").click(function(){
            var math1=Math.random();
            $("<div></div>").attr('id',math1).css({'width':'200px','height':'200px','position':'absolute','backgroundColor':'#666','float':'left','left':'500px'}).appendTo('body')
            var math2=Math.random()
            $("<span></span>").attr('id',math2).css({'width':'200px','height':'30px','backgroundColor':'red','display':'inline-block'}).appendTo($('#'+math1))  
  • 写回答

1条回答 默认 最新

  • 斯洛文尼亚旅游 2016-06-17 06:29
    关注

    Math.random得到小数包含.,.是类选择器鸟

     $("button").click(function () {
            var math1 = Math.random().toString().replace('.','_');////////////////////////
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?