例如,我希望在渲染后在此維基百科頁面中找到文本“The”的坐標,就像 chrome 的開發人員視圖給出的坐標一樣。開發者選項截圖
<div>The <b>Eagles</b> are an American <a href="/wiki/Rock_music" title="Rock music">rock</a> band formed in Los Angeles in 1971. With five number-one singles and six number-one albums, six <a href="/wiki/Grammy_Award" class="mw-redirect" title="Grammy Award">Grammy Awards</a> and five <a href="/wiki/American_Music_Award" class="mw-redirect" title="American Music Award">American Music Awards</a>, the Eagles were one of the most successful musical acts of the 1970s in North America. Founding members <a href="/wiki/Glenn_Frey" title="Glenn Frey">Glenn Frey</a> (guitars, vocals), <a href="/wiki/Don_Henley" title="Don Henley">Don Henley</a> (drums, vocals), <a href="/wiki/Bernie_Leadon" title="Bernie Leadon">Bernie Leadon</a> (guitars, vocals) and <a href="/wiki/Randy_Meisner" title="Randy Meisner">Randy Meisner</a> (bass guitar, vocals) were recruited by <a href="/wiki/Linda_Ronstadt" title="Linda Ronstadt">Linda Ronstadt</a> as band members, some touring with her, and all playing on her <a href="/wiki/Linda_Ronstadt_(album)" title="Linda Ronstadt (album)">third solo album</a>, before venturing out on their own on <a href="/wiki/David_Geffen" title="David Geffen">David Geffen</a>'s new <a href="/wiki/Asylum_Records" title="Asylum Records">Asylum Records</a> label. </div>
我已經嘗試過 innerHTML (element.innerHTML),它只回傳 div 的大小和其他一些東西,但似乎沒有什么是完全正確的。
uj5u.com熱心網友回復:
您可以使用該createRange()功能從文章中選擇您的文本。此函式選擇節點。
假設您正在使用此維基百科頁面https://en.wikipedia.org/wiki/Eagles_(band),您將不得不使用該頁面的第 7 段。
// Getting the 7th HTML paragraph element
const paragraphElement = document.querySelector('p:nth-child(7)');
const range = document.createRange();
range.setStart(paragraphElement , 0);
// Selecting the first Node only
range.setEnd(paragraphElement, 1);
const rects = range.getClientRects();
// The node location is in the rects object
console.log(rects);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420606.html
標籤:
