将属性设置为div标签 [英] Set an attribute to the div tag
本文介绍了将属性设置为div标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的代码
<div class="content"></div>
我想使用javascript来显示div标签中的内容
I want to echo something inside the div tag using javascript to show this way
<div class="content" something></div>
我需要使用javascript,因为如果屏幕宽于960px,我想使用一个函数来回显某些内容 我将此答案用于第二部分在屏幕显示时执行某些操作宽度小于960像素
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
提前感谢男人.
推荐答案
称为attribute
的属性可以使用以下方法设置.
That called attribute
, to set attribute you could use the methods below.
纯JS setAttribute()
方法:
element.setAttribute('name','value');
jQuery attr()
方法:
jQuery attr()
method :
$(element).attr('name','value');
示例:
document.querySelector('.content').setAttribute('something',''); //Pure JS
//Or
$('.content').attr('something',''); //jQuery
希望这会有所帮助.
这篇关于将属性设置为div标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文