我在 Angular 13 上使用 Ionic 6,并且我有一個 json 字串,其中包含帶有 html 代碼的全文。我需要檢查該字串中的anchor標簽和imgurl,并在需要時重寫 url 域名。
例如,我想要這個fulltext:string變數:
fulltext:string =
"<p>This is the intro text</p>
<ul>
<li><a href="downloads/category/886-download-pdf">PDF 1 <img src="images/886-download-pdf.jpg" /></a></li>
<li><a href="downloads/category/887-download-pdf">PDF 2 <img src="images/887-download-pdf.jpg" /></a></li>
<li><a href="https://website.com/downloads/category/888-download-pdf">PDF 3 <img src="https://website.com/images/888-download-pdf.jpg" /></a></li>
</ul>"
成為:
"<p>This is the intro text</p>
<ul>
<li><a href="https://website.com/downloads/category/886-download-pdf">PDF 1 <img src="https://website.com/images/886-download-pdf.jpg" /></a></li>
<li><a href="https://website.com/downloads/category/887-download-pdf">PDF 2 <img src="https://website.com/images/887-download-pdf.jpg" /></a></li>
<li><a href="https://website.com/downloads/category/888-download-pdf">PDF 3 <img src="https://website.com/images/888-download-pdf.jpg" /></a></li>
</ul>"
據我目前了解,我認為我需要執行以下操作:
anchor掃描所有標簽和標簽的變數字串img。查看這些 url 中是否已經包含域名。
如果沒有,請在這些 url 前添加域名前綴。
I tried creating a function in Typescript (or probably better creating a pipe for this), but I am unable to get this done. One of the first hurdle I have is, when I try to get the tags using querySelectorAll or getElementById, it does not work on string. I also tried declaring it as HTMLElement as per some suggestions here and I am unable to proceed through.
Can you kindly help me out here on how this can done in Angular Typescript please?
uj5u.com熱心網友回復:
這是我帶來的:https ://stackblitz.com/edit/plain-angular6-htjxrv
我通過拆分原始文本來制作舊 href 和 srcs 的最終值陣列,然后通過將您的預期路徑添加到舊 href 和 srcs 陣列的內容來構建新的 href 和 src 值陣列。最后,將文本中的舊 href 和 src 值替換為新值陣列中的元素。
我確信它可以以不同的方式完成,但我認為當你查看代碼時這已經足夠清楚了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/427224.html
標籤:angular ionic-framework angular12 angular13 ionic6
上一篇:單擊按鈕時如何關閉選定的div
