2016-02-17 28 views
0

我在跨平台解决方案(iOS,Android,MobileWeb)中使用Appcelerator。我希望能够在mobileweb中查看我UI元素的id属性。我见过如何使用Alloy设置id的例子,但我没有使用Alloy,我在JavaScript中编写了所有元素。如何在不使用Alloy时设置Appcelerator(mobileweb)中的DIV属性ID

有人可以告诉我,我必须适用于一个视图的HTML属性设置在生成的DIV的属性。

下面是一个例子观点:

this.Viewer = Ti.UI.createScrollView(
{ 
    left:25, 
    right:5, 
    bottom:5, 
    top: 0, 
    width: this.App.Width - 40, 
    height: 200, 
    contentHeight: Ti.UI.SIZE, 
    borderColor: "#333333", 
    borderWidth: 3, 
    horizontalWrap: false, 
    layout:"vertical", 
    scrollType: "vertical", 
    scrollsToTop: true, 
    showVerticalScrollIndicator: true, 
    showHorizontalScrollIndicator: false, 
    verticalBounce: false, 
    visible: true, 
    zIndex:100 
}); 

和生成的HTML

<div class="TiUIElement TiUIView TiLayoutsComposite" data-widget-id="Ti.UI.View:36" style="background-color: rgb(0, 255, 0); background-repeat: no-repeat; background-size: 100% 100%; border-color: rgb(51, 51, 51); border-width: 2px; left: 5px; top: 90px; width: 507px; height: 626px;"></div> 

回答

2

Titanium Mobile Web旨在模仿原生iOS和Android平台。由于iOS和Android没有DOM的概念,因此没有Titanium API可以暴露DOM。

换句话说,你不能设置<div>上的“id”,或者甚至不知道<div>存在。

但是,如果您绝对需要,您可以做到这一点。所有UI元素上都有一个属性,名为domNode。这仅在Titanium Mobile Web上可用。为了在iOS或Android上运行您的应用程序,您需要解决这个问题。

var myButton = Ti.UI.createButton({ title: 'click me' }); 
if (Ti.Platform.name === 'mobileweb') { 
    myButton.domNode.setAttribute('id', 'foo'); 
} 

临提示:钛移动网站无法访问浏览器的垃圾收集器,因此,如果您删除UI元素,你必须显式调用无证destroy()方法,这样的事件将被断开,DOM节点将摧毁,国家将被垃圾收集。

myButton.destroy && myButton.destroy() 
+0

理发师你的男人! - 当他提到这个例子时,请注意,当你明确指出MobileWeb时,你可以将任何标准浏览器JavaScript放入该语句中。这包括对窗口和文档对象的引用等 –

0

你就应该能够到id属性添加到你这样的视图创建功能。

this.Viewer = Ti.UI.createScrollView(
{ 
    id: 'myView', // <---- HERE IS YOUR ID 
    name: 'myView', // <---- IF YOU NEED THE `NAME` ATTRIBUTE AS WELL 
    left:25, 
    right:5, 
    bottom:5, 
    top: 0, 
    width: this.App.Width - 40, 
    height: 200, 
    contentHeight: Ti.UI.SIZE, 
    borderColor: "#333333", 
    borderWidth: 3, 
    horizontalWrap: false, 
    layout:"vertical", 
    scrollType: "vertical", 
    scrollsToTop: true, 
    showVerticalScrollIndicator: true, 
    showHorizontalScrollIndicator: false, 
    verticalBounce: false, 
    visible: true, 
    zIndex:100 
}); 
+0

你会认为这是答案,但它不是(或者我做错了什么)。我添加了一个id和一个名称属性,并且都不出现在生成的HTML中 – theThought

+0

因此,似乎自定义属性不会将其添加到HTML中。请检查这是否是[Appcelerator JIRA](https://jira.appcelerator.org/)上的已知问题。如果不是,则创建一张票据,链接到此问题,但也会在票证中提供可重复的代码,步骤和环境信息。不要忘记在这里放下一张票的链接,以便其他人可以和你一起观看。 –

相关问题