Как получить значение объекта в атрибуте? - PullRequest
0 голосов
/ 13 июля 2020

Надеюсь, с тобой все в порядке. Мне нужна ваша помощь сегодня.

У меня на странице есть a, который содержит атрибуты. Один из них, названный data-params, содержит объект. В этом объекте у нас много данных, и я хочу получить только одно значение. Я пытался использовать Object.keys OR element.attributes, но это не сработало. Я действительно не знаю, как и где искать. Кажется, этот объект выглядит как JSON.

Кстати, большое спасибо заранее за вашу помощь и удачное кодирование:)

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 \u00e0&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\u00e9... 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>

1 Ответ

1 голос
/ 13 июля 2020

Строка data-params кажется неполной. Я проделал "ремонтные работы" и обработал его, как показано ниже:

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 \u00e0&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\u00e9... Lancez la conversation !&quot;,&quot;webinarChatPlaceholder&quot;:&quot;Appuyez sur Entrer pour envoyer votre message&quot;}}">the div</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...