Javascript 在视图中按Id选择对象
在Tianium中,如何通过id选择对象 使用钛合金,我创建了一个儿童视图。我在父视图上设置了一个事件侦听器,这样就不必为每个子视图创建一个事件侦听器。在事件侦听器中,我使用Javascript 在视图中按Id选择对象,javascript,titanium,appcelerator-mobile,Javascript,Titanium,Appcelerator Mobile,在Tianium中,如何通过id选择对象 使用钛合金,我创建了一个儿童视图。我在父视图上设置了一个事件侦听器,这样就不必为每个子视图创建一个事件侦听器。在事件侦听器中,我使用e.source.id确定单击了哪个子视图。我需要更改单击视图时视图的高度,还需要更改以前打开的视图的高度。(这就是显示带有下划线类型样式的活动视图的全部内容。) 我不知道如何通过id名称选择对象,以便更改其高度 //storing the id of the last clicked feed label so we ca
e.source.id
确定单击了哪个子视图。我需要更改单击视图时视图的高度,还需要更改以前打开的视图的高度。(这就是显示带有下划线类型样式的活动视图的全部内容。)
我不知道如何通过id名称选择对象,以便更改其高度
//storing the id of the last clicked feed label so we can change it's height when it's no longer open
var feedSelector = 'selection_All';
selection_Type.addEventListener('click', function (e) {
Ti.API.info( ' ==== Select Destination Hit ==== ' + e.source.id);
if (e.source.id === feedSelector) {
//refresh the feed view
Ti.API.info('Simulating feed refresh for...' + e.source.id);
}
else {
//reducing active label height to simulatue 2px underline
e.source.setHeight(28);
//reset previous selected label height
//here's the problem
//i know how to do this in regular javascript/html
//but I don't know how to access an object by it's id in Titanium
//setting current label id in feedSelector so we can change it's height next time a button is clicked
feedSelector = e.source.id;
Ti.API.info('Changed value for feedSelector to...' + feedSelector);
}
}
}
);
var selection_All = Ti.UI.createLabel({
id: 'selection_All',
text: 'All',
width: '14.9%',
top: 0,
center: 2,
height: 28,
backgroundColor: '#fff',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
color:'#073266',
font:{fontFamily:'Trebuchet MS',fontSize:13}
});
var selection_Status = Ti.UI.createLabel({
id: 'selection_Status',
text: 'Status',
width: '22%',
top: 0,
center: 0,
height: 30,
backgroundColor: '#fff',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
color:'#073266',
font:{fontFamily:'Trebuchet MS',fontSize:13}
});
var selection_Article = Ti.UI.createLabel({
id: 'selection_Article',
text: 'Article',
width: '23%',
top: 0,
center: 0,
height: 30,
backgroundColor: '#fff',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
color:'#073266',
font:{fontFamily:'Trebuchet MS',fontSize:13}
});
var selection_Video = Ti.UI.createLabel({
id: 'selection_Video',
text: 'Video',
width: '20%',
top: 0,
center: 0,
height: 30,
backgroundColor: '#fff',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
color:'#073266',
font:{fontFamily:'Trebuchet MS',fontSize:13}
});
var selection_Audio = Ti.UI.createLabel({
id: 'selection_Audio',
text: 'Audio',
width: '20%',
top: 0,
center: 0,
height: 30,
backgroundColor: '#fff',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
color:'#073266',
font:{fontFamily:'Trebuchet MS',fontSize:13}
});
//creating selection type container and adding selection types
selection_Type.add(selection_All);
selection_Type.add(selection_Status);
selection_Type.add(selection_Article);
selection_Type.add(selection_Video);
selection_Type.add(selection_Audio);
selectionView.add(selection_Type);
Edit:我使用switch语句完成了我需要做的事情,但是如果我可以通过对象的id来获取对象,它会更干净
var feedSelector = 'selection_All';
selection_Type.addEventListener('click', function (e) {
Ti.API.info( ' ==== Select Destination Hit ==== ' + e.source.id);
if (e.source.id === feedSelector) {
//refresh the feed view if the feed is open and button is clicked
//Ti.API.info('Simulating feed refresh for...' + e.source.id);
alert('Refreshing feed');
}
else {
//reducing active label height to simulatue 2px underline
e.source.setHeight(28);
switch (feedSelector) {
case 'selection_All':
selection_All.setHeight(30);
break;
case 'selection_Status':
selection_Status.setHeight(30);
break;
case 'selection_Article':
selection_Article.setHeight(30);
break;
case 'selection_Video':
selection_Video.setHeight(30);
break;
case 'selection_Audio':
selection_Audio.setHeight(30);
break;
}
feedSelector = e.source.id;
Ti.API.info('Changed value for feedSelector to...' + feedSelector);
}
}
)) 如果您使用的是Tianium Studio,只需调试click事件并检查事件对象即可。在断点停止时,转到表达式选项卡,输入事件对象并检查它。可能UI元素作为属性存在 另一种方法是:
var labelStore = {};
function createLabel(props){
var label = Ti.UI.createLabel(props);
labelStore[props.id] = label;
return label;
}
function getLabelById(id){
return labelStore[id];
}
var selection_Status = createLabel({
id: 'selection_Status',
...
});
然后在上单击
var id = e.source.id;
var label = getLabelById(id);
..执行标签的操作如果使用的是Tianium Studio,只需调试click事件并检查事件对象即可。在断点停止时,转到表达式选项卡,输入
e
事件对象并检查它。可能UI元素是作为一个属性存在的。我使用switch语句完成了所需的操作。这是有效的,因为视图中只有五个对象需要更改,但我仍然不知道如何通过对象的id获取对象。是否有一个与常规javascript中的getObjectbyId(feedSelector)
类似的等价物?您可能喜欢DOM浏览器API而不是javascript。仅当您使用Alloy框架并使用XML文档时。我还没有在这样的场景中遇到过这样的客户,但您可以查看钛合金文档。
var id = e.source.id;
var label = getLabelById(id);