我试图简化下面的代码,使它们不那么难看。有没有更好的方法来做到这一点?非常感谢。
//the only difference is the append and after method after the $element.
if($element.is('td')){
$element.append(
$(document.createElement('div')).attr({'id': tooltipId}).addClass('js-tutorialTooltips').css({
'position': 'absolute',
'z-index':'9999',
'color':'white',
'background-color': 'rgba(0, 0, 0, 0.8)',
'border': '1px solid #cccccc',
'border-radius': '0.5em',
'box-shadow': '0 0.2em 0.2em #111'})
.append(
$(document.createElement('div')).css({'padding': '0.5em 1em'}).html(text) //for more tool info add the following + '<br/>(' + verticalPosition + '-' + horizontalPosition + ':' + domId + ')'
)
)
}else{
$element.after(
$(document.createElement('div')).attr({'id': tooltipId}).addClass('js-tutorialTooltips').css({
'position': 'absolute',
'z-index':'9999',
'width':'300px',
'color':'white',
'background-color': 'rgba(0, 0, 0, 0.8)',
'border': '1px solid #cccccc',
'border-radius': '0.5em',
'box-shadow': '0 0.2em 0.2em #111'})
.append(
$(document.createElement('div')).css({'padding': '0.5em 1em'}).html(text) //for more tool info add the following + '<br/>(' + verticalPosition + '-' + horizontalPosition + ':' + domId + ')'
)
);
}
//The only difference is prependTo and insertBefore method..
if($element.is('td')){
$(document.createElement('img'))
.attr({src:'inc/images/help_bubble.png', title:'help Image', 'class': 'helpImg'})
.prependTo($element)
.css({'position':'absolute',
'z-index':999,
'top': xPos,
'left': yPos
})
return true;
}
$(document.createElement('img'))
.attr({src:'inc/images/help_bubble.png', title:'help Image', 'class': 'helpImg'})
.insertBefore($element)
.css({'position':'absolute',
'z-index':999,
'top': xPos,
'left': yPos
})