简体   繁体   English

如何在属性中获取对象的值?

[英]How to get value of an objet in an attribute?

Hope you're well.希望你一切都好。 I need your help today.我今天需要你的帮助。

I've got on my page a which contains attributes.我在我的页面上有一个包含属性的。 One of these, named data-params, contains an object.其中一个名为 data-params,包含一个对象。 In this object we've got a lot of data and I want to get only one value.在这个对象中,我们有很多数据,我只想得到一个值。 I tried to use Object.keys OR element.attributes but it doesn't work.我尝试使用Object.keys OR element.attributes但它不起作用。 I really don't know how and where to search.我真的不知道如何以及在哪里搜索。 It seems this object looks like JSON.看起来这个对象看起来像 JSON。

By the way thanks a lot by advance for your help and happy coding :)顺便说一句,非常感谢您的帮助和愉快的编码:)

PS : Here the code of the : PS:这里的代码:

<div id="wpws-live" data-params="{&quot;cache_url&quot;:&quot;https:\\/\\/www.ecommerce-nation.fr\\/wp-content\\/plugins\\/wpwebinarsystem\\/cache\\/webinar_808227d5-f2e7-480d-b438-19f2d0e6feb8_v2.json&quot;,&quot;secure_room_name&quot;:&quot;96269b8f-3ce6-47f2-8c64-92e62291799a&quot;,&quot;secure_room_key&quot;:&quot;e2324107-c6b6-42a8-8a79-a5bdfc26ac32&quot;,&quot;reduce_server_load&quot;:false,&quot;webinar_time_in_seconds&quot;:3465657,&quot;webinar_start_time&quot;:1591194600,&quot;duration&quot;:3600,&quot;timezone_offset&quot;:120,&quot;attendee&quot;:{&quot;id&quot;:477,&quot;name&quot;:&quot;blablablo&quot;,&quot;email&quot;:&quot;blablablo@yopmail.com&quot;,&quot;is_team_member&quot;:true},&quot;translations&quot;:{&quot;webinarSummaryStartingTimer&quot;:&quot;Commence \à&quot;,&quot;webinarSummaryStartWebinar&quot;:&quot;Lancer le webinar&quot;,&quot;webinarSummaryStopBroadcasting&quot;:&quot;Stopper le webinar&quot;,&quot;webinarSummaryLogout&quot;:&quot;Quitter le webinar&quot;,&quot;webinarTimerDays&quot;:&quot;jours&quot;,&quot;webinarTimerHours&quot;:&quot;heures&quot;,&quot;webinarTimerMinutes&quot;:&quot;minutes&quot;,&quot;webinarTimerSeconds&quot;:&quot;secondes&quot;,&quot;webinarTabsPeople&quot;:&quot;Explorateurs&quot;,&quot;webinarChatEmpty&quot;:&quot;Personne n'a encore parl\é... Lancez la conversation !&quot;,&quot;webinarChatPlaceholder&quot;:&quot;Appuyez sur Entrer pour envoyer votre message&quot;,&quot;webinarChatMenuDelete&quot</div></div></div></div></div></div></div></div></div></main></div>

The data-params string seems to be incomplete. data-params字符串似乎不完整。 I did some "repair work" on it and managed to process it as shown below:我对它做了一些“修复工作”并设法处理它,如下所示:

 let d=document.querySelector("#wpws-live"),jsn=d.dataset.params, o=JSON.parse(jsn); d.textContent=o.attendee.email; // display a single property in the div console.log(o); // list the whole object with all its properties
 .as-console-wrapper { max-height: 80% !important;}
 <div id="wpws-live" data-params="{&quot;cache_url&quot;:&quot;https:\\/\\/www.ecommerce-nation.fr\\/wp-content\\/plugins\\/wpwebinarsystem\\/cache\\/webinar_808227d5-f2e7-480d-b438-19f2d0e6feb8_v2.json&quot;,&quot;secure_room_name&quot;:&quot;96269b8f-3ce6-47f2-8c64-92e62291799a&quot;,&quot;secure_room_key&quot;:&quot;e2324107-c6b6-42a8-8a79-a5bdfc26ac32&quot;,&quot;reduce_server_load&quot;:false,&quot;webinar_time_in_seconds&quot;:3465657,&quot;webinar_start_time&quot;:1591194600,&quot;duration&quot;:3600,&quot;timezone_offset&quot;:120,&quot;attendee&quot;:{&quot;id&quot;:477,&quot;name&quot;:&quot;blablablo&quot;,&quot;email&quot;:&quot;blablablo@yopmail.com&quot;,&quot;is_team_member&quot;:true},&quot;translations&quot;:{&quot;webinarSummaryStartingTimer&quot;:&quot;Commence \à&quot;,&quot;webinarSummaryStartWebinar&quot;:&quot;Lancer le webinar&quot;,&quot;webinarSummaryStopBroadcasting&quot;:&quot;Stopper le webinar&quot;,&quot;webinarSummaryLogout&quot;:&quot;Quitter le webinar&quot;,&quot;webinarTimerDays&quot;:&quot;jours&quot;,&quot;webinarTimerHours&quot;:&quot;heures&quot;,&quot;webinarTimerMinutes&quot;:&quot;minutes&quot;,&quot;webinarTimerSeconds&quot;:&quot;secondes&quot;,&quot;webinarTabsPeople&quot;:&quot;Explorateurs&quot;,&quot;webinarChatEmpty&quot;:&quot;Personne n'a encore parl\é... Lancez la conversation !&quot;,&quot;webinarChatPlaceholder&quot;:&quot;Appuyez sur Entrer pour envoyer votre message&quot;}}">the div</div>

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

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