[英]Set an attribute to the div tag
I have a simple code 我有一个简单的代码
<div class="content"></div>
I want to echo something inside the div tag using javascript to show this way 我希望使用javascript来回显div标签内的某些内容,以此方式显示
<div class="content" something></div>
I need it using javascript because I want to use a function to echo something if the screen is wider than 960px I used this answer for the second part Do something if screen width is less than 960 px 我需要使用javascript,因为我想使用一个函数来回应一些东西,如果屏幕宽度超过960px我使用这个答案的第二部分做一些事情,如果屏幕宽度小于960像素
Thanks guy in advance. 提前谢谢你。
That called attribute
, to set attribute you could use the methods below. 那个叫
attribute
,设置属性你可以使用下面的方法。
Pure JS setAttribute()
method : 纯JS
setAttribute()
方法:
element.setAttribute('name','value');
jQuery attr()
method : jQuery
attr()
方法:
$(element).attr('name','value');
Example : 示例:
document.querySelector('.content').setAttribute('something',''); //Pure JS
//Or
$('.content').attr('something',''); //jQuery
Hope this helps. 希望这可以帮助。
使用jQuery:
$('.content').attr('something','');
If you want to add the attribute if window width is greater then 960 如果要在窗口宽度大于960时添加属性
if ( window.innerWidth > 960 ) {
document.querySelector('.content').setAttribute('something','')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.