Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在typescript中将文本字符串转换为html对象_Javascript_Angular_Typescript_Dom - Fatal编程技术网

Javascript 如何在typescript中将文本字符串转换为html对象

Javascript 如何在typescript中将文本字符串转换为html对象,javascript,angular,typescript,dom,Javascript,Angular,Typescript,Dom,在我的Angular应用程序下: 我有一个字符串,如下所示: let mystr : "<p>Ceci c'est un texte aléatoire de test <a href='https://codemirror.net' title='https://codemirror.net' target='_blank'>ceci c'est un simple HYPERLINK</a> Ceci c'est un autre texte a

在我的Angular应用程序下:

我有一个字符串,如下所示:

let mystr : "<p>Ceci c'est un texte aléatoire de test <a href='https://codemirror.net' title='https://codemirror.net' target='_blank'>ceci c'est un simple HYPERLINK</a> Ceci c'est un autre texte aléatoire de test Ceci c'est un autre texte aléatoire de test <a title='' target='_blank' href='http://testing.com/book.html?default=<script>alert(document.cookie)</script>'>Un autre hyperlink </a></p><p>Ceci c’est un hyperlink copié sur la même page:</p><p><a href='https://aaa.bb.tes.com/aide-et-support/fiche-assistance/31516/une-page-grise-saffiche-a-louverture-dun-fichier-excel-2010' title='se rendre sur la page Une page grise s’affiche à l’ouverture d’un fichier – Excel 2010'><span style='color:blue;'>Une page grise s’affiche à l’ouverture d’un fichier – Excel 2010</span></a></p>"
让我告诉自己:Ceci c'est un texte aléatoire de test Ceci c'est un autre texte aléatoire de test Ceci c'est un autre texte aléatoire de test

Ceci c'est un hyperlink copi sure la même page:

我的目的是,如何将该字符串转换为html对象,并在typescript中操作它们

(beofre使用innerHtml将它们附加到DOM中

然后可以在视图中显示之前对其进行修改

例如,我想更改该字符串中所有标签的所有标题属性

const dom=new DOMParser().parseFromString('Hi','text/html'))
常量h1=dom.querySelector('h1')
h1.setAttribute('hidden','true')
console.log(h1)//Hi
您可以使用

const dom=new DOMParser().parseFromString('Hi','text/html'))
常量h1=dom.querySelector('h1')
h1.setAttribute('hidden','true')
console.log(h1)//Hi
例如,您可以:

const mystr : "<p>Ceci c'est un texte aléatoire de test <a href='https://codemirror.net' title='https://codemirror.net' target='_blank'>ceci c'est un simple HYPERLINK</a> Ceci c'est un autre texte aléatoire de test Ceci c'est un autre texte aléatoire de test <a title='' target='_blank' href='http://testing.com/book.html?default=<script>alert(document.cookie)</script>'>Un autre hyperlink </a></p><p>Ceci c’est un hyperlink copié sur la même page:</p><p><a href='https://aaa.bb.tes.com/aide-et-support/fiche-assistance/31516/une-page-grise-saffiche-a-louverture-dun-fichier-excel-2010' title='se rendre sur la page Une page grise s’affiche à l’ouverture d’un fichier – Excel 2010'><span style='color:blue;'>Une page grise s’affiche à l’ouverture d’un fichier – Excel 2010</span></a></p>"
    
    const el = document.createElement('div');
    el.innerHTML = mystr;
    const allA = el.querySelectorAll('a');
    
    allA.forEach(a => console.log(a.getAttribute('title')));
const mystr:Ceci c'est un texte aléatoire de test Ceci c'est un autre texte aléatoire de test Ceci c'est un autre texte aléatoire de test
const el=document.createElement('div');
el.innerHTML=mystr;
常量allA=el.queryselectoral('a');
allA.forEach(a=>console.log(a.getAttribute('title'));
诸如此类……

例如,您可以:

const mystr : "<p>Ceci c'est un texte aléatoire de test <a href='https://codemirror.net' title='https://codemirror.net' target='_blank'>ceci c'est un simple HYPERLINK</a> Ceci c'est un autre texte aléatoire de test Ceci c'est un autre texte aléatoire de test <a title='' target='_blank' href='http://testing.com/book.html?default=<script>alert(document.cookie)</script>'>Un autre hyperlink </a></p><p>Ceci c’est un hyperlink copié sur la même page:</p><p><a href='https://aaa.bb.tes.com/aide-et-support/fiche-assistance/31516/une-page-grise-saffiche-a-louverture-dun-fichier-excel-2010' title='se rendre sur la page Une page grise s’affiche à l’ouverture d’un fichier – Excel 2010'><span style='color:blue;'>Une page grise s’affiche à l’ouverture d’un fichier – Excel 2010</span></a></p>"
    
    const el = document.createElement('div');
    el.innerHTML = mystr;
    const allA = el.querySelectorAll('a');
    
    allA.forEach(a => console.log(a.getAttribute('title')));
const mystr:Ceci c'est un texte aléatoire de test Ceci c'est un autre texte aléatoire de test Ceci c'est un autre texte aléatoire de test
const el=document.createElement('div');
el.innerHTML=mystr;
常量allA=el.queryselectoral('a');
allA.forEach(a=>console.log(a.getAttribute('title'));
等等