我目前有两张图片和两个条件。
目标 如果我的页面的正文类为(class),请在htmlWrap变量后附加更新的imgStr文件名,以显示该页面的正确图表。
问题 结果对于imgStr是未定义的,因此它使用/ undefined而不是/gi-chart-2.png来显示URL。如果将变量移到函数之外,但是无法基于if语句切换变量值(图像名称)。
代码
// Check the body class
$( "body" ).each(function() {
var htmlWrap = '<div class="gi-chart-wrap"><span class="eltdf-title-holder reviews_tab ui-accordion-header ui-state-default ui-corner-all" role="tab" id="ui-id-3" aria-controls="ui-id-4" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="eltdf-accordion-mark"><span class="eltdf_icon_plus icon_plus"></span><span class="eltdf_icon_minus icon_minus-06"></span></span><span class="eltdf-tab-title">GI Chart</span></span></div><div class="eltdf-accordion-content ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" id="ui-id-4" aria-labelledby="ui-id-3" role="tabpanel" aria-hidden="true" style="display: none;"><img src="/wp-content/uploads/2019/01/' +imgStr+ '" alt="GI Chart" /></div></div>';
// If the body has the class append htmlWrap with the correct imgStr variable
if ( $( this ).hasClass( "product-fruit-nuts-superfood-with-baobab" ) ) {
$( this )
var imgStr = 'gi-chart.png';
$(".eltdf-accordion-holder").each(function() {
$(this).append(htmlWrap);
});
} else if ( $( this ).hasClass( "product-dark-chocolate-mandarin" ) ) {
$( this )
var imgStr = 'gi-chart-2.png';
$(".eltdf-accordion-holder").each(function() {
$(this).append(htmlWrap)
});
}
});
答案 0 :(得分:0)
在添加前,将imgStr替换为图像URL。
s = pd.Series([0,0,0,0,0,0,0,0,0,1,1]*5, index=pd.date_range('2010-01-01', freq='MS', periods=55))
s2 = s*0
gps = s.ne(s.shift(1)).where(s.astype(bool)).cumsum()
idx = s[::-1].groupby(gps).idxmax()
#1.0 2010-11-01
#2.0 2011-10-01
#3.0 2012-09-01
#4.0 2013-08-01
#5.0 2014-07-01
#dtype: datetime64[ns]
to_one = np.hstack(pd.date_range(start=x+pd.offsets.DateOffset(months=1), freq='MS', periods=6) for x in idx)
s2[s2.index.isin(to_one)]= 1
# I check .isin in case the indices extend beyond the indices in s2