简体   繁体   中英

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

I have a div tag like that:

<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.

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:

<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').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 ) :

<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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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