简体   繁体   English

Javascript将属性赋予元素

[英]Javascript put attribute to a element

I'm using uikit and to open a dropdown i can set some attributes like: 我正在使用uikit并打开一个下拉菜单,我可以设置一些属性,例如:

data-uk-dropdown="{mode:'click',justify:'#my-id',}" 

this kind of way it's usable from html view in the input. 这种方式可以在输入的html视图中使用。 Is it possible use the justify directly from javascript? 是否可以直接从javascript使用justify I mean something like: 我的意思是:

 $(document).on('focusin', '#my-id', function () {
        $('.uk-dropdown').justifly('#my-id');
    });

of course $('.uk-dropdown').justifly('#my-id'); 当然$('.uk-dropdown').justifly('#my-id'); it's not the correct way and it's not working. 这不是正确的方法,也没有用。

You need to use .attr() for setting the attribute value: 您需要使用.attr()来设置属性值:

 $('.uk-dropdown').attr('justifly','#my-id');

for setting multiple attributes: 用于设置多个属性:

  $('.uk-dropdown').attr({'mode':'click','justify':'#my-id'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM