简体   繁体   English

打印div标签标题和其他内容的更简单方法

[英]Simpler way to Print title of a div tag and other contents

I have a div tag like that: 我有一个像这样的div标签:

<div class="This is a class" title="Server:http://servername/
Area: PrOD
User: USER NAME"> X</Div>.

I want to print title, area and user from the above div tag. 我想从上面的div标签打印标题,区域和用户。

I found couple of threads which are not clear. 我发现几个不清楚的线程。

Any help is appreciated. 任何帮助表示赞赏。

If you are not using the data-* attributes and your div would look like this: 如果不使用data-*属性,并且div如下所示:

<div class="myDiv" id="original" title="http://servername" area="PrOD" user="tester"></div>

then check this demo which get's all the div's attributes: 然后检查此演示,该演示获得了所有div的属性:

 $('div').each(function(){ var attributes = this.attributes; $.each(attributes, function(ndx){ var attrName = attributes[ndx].name; var attrVal = attributes[ndx].value; // add conditions for certain attribute name; console.log(attributes[ndx].name + ' = ' + attributes[ndx].value); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myDiv" id="original" title="http://servername" area="PrOD" user="tester">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde, alias.</div> 

Or with the aid of data-* attributes (you will need to update the necessary attributes to start with the data- tag eg data-title , data-area , data-user ) : 或借助data-*属性(您将需要更新必要的属性,以data-title tag开头,例如data-titledata-areadata-user ):

<div class="anotherDiv" id="original2" data-title="http://servername" data-area="PrOD" data-user="tester"></div>

then check this demo: 然后查看此演示:

 var datas = $('div.anotherDiv').data(); $.each(datas, function(ndx){ console.log(ndx + ' = ' + datas[ndx]); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="anotherDiv" id="original2" data-title="http://servername" data-area="PrOD" data-user="tester">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde, alias.</div> 

Hope this helps. 希望这可以帮助。 :) :)

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

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