在这里你去:一个更新的小提琴提供你的增强。:) 这假设您没有更改元素 id 的命名约定。
// Newly added code.
// Automatically update the iphone display when
// the user types.
$('#tabContent').on('keyup', 'input[id^="page"], textarea[id^="page"]', function () {
var _thisId = $(this).attr('id');
// Parse out the section from the id: (e.g. Title from pageTitle_tab1)
// and lowercase it to match the naming conventions in this document.
_thisId = _thisId.substring(4, _thisId.indexOf('_')).toLowerCase();
// Only 1 preview element so just modify it's html.
// _thisId will be title|subtitle|content
$('#' + _thisId + 'Preview').html('<h1>' + $(this).val() + '</h1>');
});
// Generic handler for all tabs, except the first "add tab"
$('#tabHeaders').on('click', 'li:not(:first-child)', function () {
var index = $(this).find('a').attr('href').split('#tab')[1];
// index is the tab number, grab our iphone divs and
// iterate through each one.
$('div#iphone-frame div[id$="Preview"]').each(function (idx, el) {
// The next two lines determine which #page[text field]_tab[index]
// input to grab our text value from.
var whichId = $(this).attr('id').split('Preview')[0];
whichId = whichId.charAt(0).toUpperCase() + whichId.slice(1);
// Get the text value and set it to the corresponding iphone display.
var textVal = $('#page' + whichId + '_tab' + index).val();
$(this).html('<h1>' + textVal + '</h1>');
});
});
// End newly added code
http://jsfiddle.net/u7S5P/31/
这样,您在打开预览时不需要更改“iphone”值的逻辑,因为它们会在用户键入时即时更新。