我们可以从打字稿代码中更改图像源吗?

时间:2019-07-19 08:24:21

标签: javascript html typescript dom

我从简单的Typescript(.ts)+ HTML(.js)代码开始进行练习。 在HTML页面中,我具有以下标记以显示某些图像:

<img id="myimage" src='images/samsung_edge_silver.jpg' height="250px">
<div><b><span id="Name"></b></div>

现在我有ts文件来更改此代码中的字段。 我可以使用以下命令更改跨度字段名称的值:

document.getElementById("pName").innerHTML = productName;

但是有什么办法可以更改myimage的源URL值。

我读了一些答案,人们给出的答案大多是关于角度的。 但这是纯typescript + html。 我们如何为图像源分配一个新值?

3 个答案:

答案 0 :(得分:1)

您可以这样设置。

document.getElementById('myimage').src = 'http://yourImagePathHere';

检出小提琴here

答案 1 :(得分:1)

您必须这样做:

(document.getElementById('test') as HTMLImageElement).src = 'http://www.image.com';

这样做的原因是document.getElementById('id')将返回HTMlElement,并且并非所有HTMLElement都具有src属性。

答案 2 :(得分:0)

如果您使用的是角度,则可以执行以下操作:

HTML:

08:35:51,817 ERROR [main][java:355] PARSER_ERROR
 [echo] D:\Gitlab\OR3-Dev-Core\liferay-plugins-sdk-6.2-ee-sp17\portlets\onrule-extension-projects-task-service-portlet\ServiceBuilder.temp:1684:13: expecting SEMI, found 'getDonutChartData'
 [echo]     at antlr.Parser.match(Parser.java:211)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.classField(InternalJavaParser.java:3081)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.classBlock(InternalJavaParser.java:2457)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.classDefinition(InternalJavaParser.java:710)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.typeDefinitionInternal(InternalJavaParser.java:632)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.typeDefinition(InternalJavaParser.java:465)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.parse(InternalJavaParser.java:296)
 [echo]     at de.hunsicker.jalopy.language.JavaRecognizer.parse(JavaRecognizer.java:588)
 [echo]     at de.hunsicker.jalopy.Jalopy.parse(Jalopy.java:1212)
 [echo]     at de.hunsicker.jalopy.Jalopy.format(Jalopy.java:1044)
 [echo]     at de.hunsicker.jalopy.Jalopy.format(Jalopy.java:1017)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.writeFile(ServiceBuilder.java:448)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.writeFile(ServiceBuilder.java:364)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder._createServiceSoap(ServiceBuilder.java:3236)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:810)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:229)
 [echo] 08:35:51,942 ERROR [main][io:1071] UNKNOWN_ERROR
 [echo] expecting EOF, found 'return'
 [echo] D:\Gitlab\OR3-Dev-Core\liferay-plugins-sdk-6.2-ee-sp17\portlets\onrule-extension-projects-task-service-portlet\ServiceBuilder.temp:1719:33: expecting EOF, found 'return'
 [echo] Nested Exception is: D:\Gitlab\OR3-Dev-Core\liferay-plugins-sdk-6.2-ee-sp17\portlets\onrule-extension-projects-task-service-portlet\ServiceBuilder.temp:1719:33: expecting EOF, found 'return'  at antlr.Parser.match(Parser.java:211)
 [echo]     at antlr.Parser.match(Parser.java:211)
 [echo]     at de.hunsicker.jalopy.language.antlr.InternalJavaParser.parse(InternalJavaParser.java:308) at de.hunsicker.jalopy.language.antlr.InternalJavaParser.parse(InternalJavaParser.java:308)
 [echo]     at de.hunsicker.jalopy.language.JavaRecognizer.parse(JavaRecognizer.java:588)
 [echo]     at de.hunsicker.jalopy.Jalopy.parse(Jalopy.java:1212)   at de.hunsicker.jalopy.language.JavaRecognizer.parse(JavaRecognizer.java:588)
 [echo]     at de.hunsicker.jalopy.Jalopy.format(Jalopy.java:1044)
 [echo]     at de.hunsicker.jalopy.Jalopy.parse(Jalopy.java:1212)
 [echo]     at de.hunsicker.jalopy.Jalopy.format(Jalopy.java:1017)  at de.hunsicker.jalopy.Jalopy.format(Jalopy.java:1044)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.writeFile(ServiceBuilder.java:448)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.writeFile(ServiceBuilder.java:364)    at de.hunsicker.jalopy.Jalopy.format(Jalopy.java:1017)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder._createServiceSoap(ServiceBuilder.java:3236)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:810)   at com.liferay.portal.tools.servicebuilder.ServiceBuilder.writeFile(ServiceBuilder.java:448)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:229) at com.liferay.portal.tools.servicebuilder.ServiceBuilder.writeFile(ServiceBuilder.java:364)
 [echo] Building ProjectTaskDependency
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder._createServiceSoap(ServiceBuilder.java:3236)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:810)
 [echo]     at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:229)

打字稿:

<img [src]="mySource">