温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - UI5: How to access html element defined inside view?
html javascript sapui5

javascript - UI5:如何访问视图内定义的html元素?

发布于 2020-04-18 10:43:07

我有一个UIComponent与此视图:

<mvc:View controllerName="my.components.webmap.controller.Map"
   xmlns:mvc="sap.ui.core.mvc"
   xmlns="sap.m"
   xmlns:html="http://www.w3.org/1999/xhtml">
   <html:div id='myDiv' style='height:inherit'></html:div>
   <Button id="helloWorld" text="Say Hello" press=".onShowHello" />
</mvc:View>

在View控制器中,我可以从中获取Button及其ID

this.getView().byId("helloWorld").sId

但我无法使用该byId()方法获取div的ID

this.getView().byId("myDiv"); // returns undefined

如何获取div的ID以访问元素?我需要div的ID,以便可以向其添加一些其他的第三方控件。

查看更多

提问者
user1025184
被浏览
41
9,651 2020-02-03 17:47

您可以使用createId 将您的lokal ID转换为全局ID,然后将其用于getElementById

// After rendering
var divId = this.createId("myDiv"); // this == controller instance
document.getElementById(divId).whatEver