我在一个客户的汽车销售网站上工作,他们与第三个 Pty 供应商签订了合同,该供应商提供了一个脚本,可以在他们的库存结果和详细信息页面上运行。
库存清单结果和详细信息页面是数据驱动的,从数据库源动态提取车辆数据。
我想要做什么:
- 页面加载后,将此第 3 方代码动态插入到每个库存清单的 DIV 容器中,随后为每个详细信息页面插入。
这是我试图动态插入每个车辆列表的第三个 PTY 代码块:
<div class="lowPrice">
<a href="javascript:displayLoanInfo(vehicleVIN, SourceID);" id="lbl'+vehicleVIN+'"></a>
<script type="text/javascript">
GetLowestPayment('vehicleVIN', 'SourceID', "lbl'+vehicleVIN+'");
function AssignLowestPayment(LowestPaymentValue, ClientLabelID) {
if (LowestPaymentValue !== '') {
document.getElementById(ClientLabelID).innerHTML = LowestPaymentValue;
};
};
</script>
我已经设置了一个 .JS 文件以通过 $(getScript) 函数加载到客户端脚本中,并且我知道足够的 JQuery 能够从页面中获取每辆车的数据元素并传递给函数(我相信我已经正确地做到了)。但是,我被困在需要插入上述代码并让那些包含必要数据元素的变量正确传递给它的地方。
这是我迄今为止编写的 .JS 文件中的代码:
var loc = document.location;
var endrive = /inventory-results|inventory-details/.test(loc.pathname);
var SourceID = '10057', ChromeStyleID = '', vehicleZipCode='06473', mode = 'text',vehicleCondition = "",lresult,dresult;
if(/selectedcat\/new/.test(loc.pathname)){ vehicleCondition = "New";}
if(/selectedcat\/preowned/.test(loc.pathname)){ vehicleCondition = "Preowned";}
if(/inventory-results/.test(loc.pathname)){ divblock = '.piiResultsContainer'; lresult = true;cnt=-1}
if(/inventory-details/.test(loc.pathname)){ divblock = '#dt_vehicle_tbl';dresult= true;cnt=-1}
var vdata = [], vattr,vehicleVIN,vehicleStockNum,vehicleTransmission,vehicleOdometer,vehicleExtColor,vehiclePrice,vehicleStyle,vehiclePhoto,vehicleYear,vehicleMake,vehicleModel,vehicle;
if(endrive){
$.getScript("http://www.velocitylogix.com/OfferLogix.js?width=960px&height=700&pack=false",function(){
$(divblock).each(function(){
if(lresult){
vdata['vehiclePrice'] = $(this).find('#ulPricing li:first-child').text().trim();
attrsearch = 'ul#att_listL li';
SourceID = '10057';
}
if(dresult){
vdata['vehiclePrice'] = $(this).find('#dt_vehicle_listing').find('ul#ulPricing li:first-child').text().trim();
attrsearch = '#dt_vehicle_attribute ul#att_listL li';
SourceID = '10057';
}
if(/MSRP/.test(vdata['vehiclePrice'])){ vdata['vehiclePrice'] = vdata['vehiclePrice'].replace("MSRP: ","");}
if(/Selling Price/.test(vdata['vehiclePrice'])){ vdata['vehiclePrice'] = vdata['vehiclePrice'].replace("Selling Price: ","");}
var tlist = $(this).find(attrsearch).each(function(){
vattr =$(this).find('span').text();
switch(vattr){
case "VIN:":vdata['vehicleVin']=$(this).text().replace("VIN: ","");
break;
case "Stock #:":vdata['vehicleStockNum']=$(this).text().replace("Stock #: ","");
break;
case "Miles:":vdata['vehicleOdometer']=$(this).text().replace("Miles: ","");
break;
default :
vdata['vehicleOdometer']= 0;
break;
}
vehiclePrice = vdata['vehiclePrice'];
vehicleVIN = vdata['vehicleVin'];
});
cnt++;
GetLowestPayment(vehicleVIN, SourceID, 'lbl' + vehicleVIN);
function AssignLowestPayment(LowestPaymentValue, ClientLabelID) {
if(LowestPaymentValue !== '') {
document.getElementById(ClientLabelID).innerHTML = LowestPaymentValue;
};
};
$(this).find('#ulPricing').append('<div style="max-width:260px; position:relative;z-index: auto;"><div id="din_'+cnt+'" style="white-space: nowrap;"></div><div class="lowPrice"><a href="javascript:displayLoanInfo(vehicleVIN, SourceID);" id="lbl'+vehicleVIN+'"></a></div></div>');
});
});
}
任何帮助将不胜感激。