2016-02-17 61 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/)上的已知問題。如果不是,則創建一張票據,鏈接到此問題,但也會在票證中提供可重複的代碼,步驟和環境信息。不要忘記在這裏放下一張票的鏈接,以便其他人可以和你一起觀看。 –

相關問題