Devil1316snow 2016-11-05 07:15 采纳率: 66.7%
浏览 974
已采纳

求助,想用js为div框(thinkphp 循环出来的列表项) 设置一些属性

页面:

 <volist name='box' id='vo'>
<div>
<div class="file-item ng-scope" type-data="{$vo.box_type}" id-data="{$vo.box_id}">
<div class="task-cell-check" style="width: 4%;">
<input type="checkbox" class="checkeds" name="box_id" value="{$vo.box_id}"/>
</div>
<div class="tuodong" style="width: 20%; margin-left: -65px;" ondblclick='ajax_get_content("__MODULE__/Wangpan/index/box_id/{$vo.box_id}")'>
<switch name='vo.box_type'>
<case value='1'><img src="__IMAGES__/wenjianjia.png" style="height: 28px; margin-top: -5px; margin-left: -40px; 
" ></case>
<case value='1'><img src="__IMAGES__/wenjianjia.png" style="height: 28px; margin-top: -5px; margin-left: -40px; 
" ondrop="drop(event)" ondragover="allowDrop(event)" ></case>
<case value='2'><img src="__IMAGES__/tpys.png" style="height: 28px; margin-top: -5px; "></case>
<case value='3'><img src="__IMAGES__/txt.png" style="height: 28px; margin-top: -5px; "></case>
<case value='4'>(其他)</case>
<default />未添加类型
</switch>
{$vo.box_mc}
</div>
<div class=" ng-binding" style="width: 20%;" ondblclick='ajax_get_content("__MODULE__/Wangpan/index/box_id/{$vo.box_id}")'> {$vo.users_truename}</div>

</div>
</div>
</volist>

js:
 $(document).ready(function(){

        var type= $('.file-item').attr('type-data');
        console.log(type);
        $('.file-item').attr({
            "draggable" : "true",
            "ondragstart" : "drag(event)"
        });
                //想实现的功能
        if (type=='1') {

            $(this).attr({

            "ondrop" : "drop(event)",
            "ondragover" : "allowDrop(event)"
          });
         }


    })

  • 写回答

1条回答 默认 最新

  • 斯洛文尼亚旅游 2016-11-05 10:22
    关注

    each变量,要不调用attr只能得到第一个dom对象,而不是当前对象的

    
        $(document).ready(function () {
            $('.file-item').each(function () {
                var type = $(this).attr('type-data');////////
                console.log(type);
                $(this).attr({/////////////
                    "draggable": "true",
                    "ondragstart": "drag(event)"
                });
                //想实现的功能
                if (type == '1') {
    
                    $(this).attr({///////////
    
                        "ondrop": "drop(event)",
                        "ondragover": "allowDrop(event)"
                    });
                }
    
            });
        })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?