【问题标题】:How to dynamically target UI Elements in Appcelerator?如何在 Appcelerator 中动态定位 UI 元素?
【发布时间】:2016-07-30 08:37:33
【问题描述】:

使用合金元素很容易定位

views.xml:

<Label id="targetID1"/>
<Label id="targetID2"/>
<Label id="targetID3"/>

controller.js:

$.targetID1.backgroundColor = "red";
$.targetID2.backgroundColor = "green";
$.targetID3.backgroundColor = "blue";

但是有没有办法将目标 ID 动态传递给函数并在该函数中设置值? 特别是,我想更改最后选择的对象的背景颜色。

例如:

var selectedObject;

function clickOnObject(e) {
selectedObject = e.source.id;
return selectedObject;
}

changeBackgroundColor(selectedObject)

//should change the background color of the selected object passed to the function

function changeBackgroundColor(id) {
    $.id.backgroundColor = "orange" //this does not work
}

我找到了这个 (Select dynamically generated element by id in Titanium Appcelerator),但我不确定这是否是同一件事。

我有多个字段并使用了 switch 语句。这当然是相当麻烦的。

【问题讨论】:

    标签: titanium-alloy appcelerator-titanium


    【解决方案1】:

    在您的情况下,您可以使用 selectedObject = e.source 没有 id。然后你的变量中有整个对象。在 changeBackgroundColor 中,您将使用不带 $ 的 id。

    例如这行得通:

    var obj;
    
    function fn(){
        obj.title = "testasdf"
    }
    
    $.btn1.addEventListener("click",function(e){
        obj = e.source;
        fn();
    
    });
    $.btn2.addEventListener("click",function(e){
        obj = e.source;
        fn();
    });
    

    在 index.xml 中创建了两个按钮。但是您可以在没有var obj 的情况下使用它,只需将 e.source 作为参数传递给fn()。取决于您的用例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      相关资源
      最近更新 更多