Javascript将信息传递给HTML
我有一个主页,并且页面上有项目。当我单击某个项目上的“立即订购”时,HTML页面会滑过页面的一部分以显示该项目的详细信息。我无法将单击的项目与包含每个项目的详细信息的对象数组内的项目链接起来,因此我可以在滑过的页面上显示详细信息。Javascript将信息传递给HTML
这里的主页:
<div class="col-md-4 col-sm-6">
<div class="shop-item">
<div class="shop-thumbnail">
<img src="img/originalImage.jpg" alt="">
</div>
<div class="shop-item-footer">
<div class="shop-item-details">
<h3 class="shop-item-title">Original Title</h3>
<span class="shop-item-price">
$50.00
</span>
</div>
<div class="shop-item-order-btn">
<a href="ajax/slidingpage.html" class="btn btn-ghost btn-sm btn-pill btn-primary waves-effect waves-light ajax-load-link" data-id="003" onclick="changeImage()">Order Now</a>
</div>
</div>
</div>
</div>
在精细页幻灯片,但我似乎无法获得图像改变。具有滑出屏幕的信息的html文件全部包含在文章标签中。下面是带有图片的HTML代码片断,我似乎无法改变。
<article class="order-details">
<div class="order-totals">
<div class="product-image">
<!-- ID for image -->
<img id="pageOrder" src="../img/Image1.jpg" alt="">
</div>
</div>
</article>
这是我的JS到目前为止,这是行不通的。
var items = [{ name: 'Black', cost: '$10.00', id: '001', src: '../img/Black.jpg' }, { name: 'Something', cost: '$10.00', id: '002', src: '../img/image2.jpg' },
function changeImage() {
for (var i = 0; i >= items.count; i++) {
if (i = items.count) {
document.getElementById('pageOrder').src = items.src;
}
}
};
如果我理解正确的话,当你点击的顺序按钮,可以通过读取被点击,然后从这样的项目访问属性的命令按钮的data-id
财产找到ID在items
项目所以:
为了使你的特定场景能够正常工作,你将不得不修改你原来的问题中没有包含的一些功能,但是我从你在评论中提供的链接中找到了一些功能。该代码将如下所示:
var items = [{
name: 'Black',
cost: '50.00',
id: '001',
src: 'img/Black2.jpg'
}, {
name: 'Red',
cost: '50.00',
id: '002',
src: 'img/BloodRed2.jpg'
}, {
name: 'Desert Fox',
cost: '50.00',
id: '003',
src: 'img/DesertFox.jpg'
}];
// Single Post via Ajax
//------------------------------------------------------------------------------
var ajaxLoadLink = $('.ajax-load-link'),
orderBackdrop = $('.order-content-backdrop'),
orderContainer = $('.order-content-wrap'),
orderContentWrap = $('.order-content-wrap .inner'),
orderContent = $('.order-content-wrap .inner .order-content'),
closeBtn = $('.order-content-wrap .close-btn'),
orderPreloader = $('.order-content-wrap .preloader');
// Open Order
function openOrder(itemDetails) {
$('body').addClass('order-content-open');
orderBackdrop.addClass('active');
orderContainer.addClass('open');
orderPreloader.addClass('active');
orderContentWrap.addClass("loaded");
setTimeout(function() {
orderPreloader.removeClass('active');
// insert data from itemDetails into their respective places on the form
$(".order-content-wrap .order-details .product-image img").attr("src", itemDetails.src);
$(".order-content-wrap .order-details .product-image img").attr("alt", itemDetails.name);
$(".order-content-wrap .product-title").text(itemDetails.name);
$(".order-content-wrap .price i").text(itemDetails.cost);
$(".order-content-wrap .total-cost i").text(itemDetails.cost);
}, 900);
}
// Close Order
function closeOrder() {
$('body').removeClass('order-content-open');
orderBackdrop.removeClass('active');
orderContainer.removeClass('open');
orderContentWrap.removeClass('loaded');
}
ajaxLoadLink.on('click', function(e) {
var itemDetails = items.find(item => item.id === $(this).attr('data-id'));
openOrder(itemDetails);
e.preventDefault();
});
Working Demo
- 代码太长把堆栈中的片段
- 确保按“用JS运行”当你加载jsbin
滑过的页面与该代码完全空白。上面发布的div和article段是分开的,因为它们在不同的html文件中。 – TomG103
@ TomG103你将不得不发布你的代码,使“滑入”和数据人口发生。 – mhodges
。order-totals { \t display:table; \t宽度:100%; padding-bottom:10px; \t .product-image, \t。order-totals-detail { \t \t display:table-cell; \t \t vertical-align:top; \t} \t .product-image { \t \t width:325px; \t \t padding-right:25px; \t \t> img {width:100%; } \t} .product-title { margin-bottom:floor($ line-height-computed/3); } 。order-quantity { margin-bottom:floor($ line-height-computed/4); > * { display:inline-block; vertical-align:middle; margin-right:6px; } span {font-size:$ font-size-lead; } } – TomG103
'项目'是什么样的。它看起来好像你试图把它看作一个数组,它将是'.length'而不是'.count'。 if是做一个赋值而不是比较('x === y'),如果这些项是一个数组,'items.src'没有意义。编辑:和'I> = items.count'与我从0开始永远不会是真的 – Taplar
我创建了一个数组与密钥所需的信息传递与图像。我忘了在代码中添加数组,但我在这个问题上被烧毁了。 VAR项目= {[ 名: '黑', 费用: '$ 10.00', ID: '001', SRC: '../img/Black.jpg' },{ 名 :“ Something', cost:'$ 10.00', id:'002', src:'../img/image2.jpg' }, – TomG103
@Taplar for循环也从0开始,然后检查'i > =长度,除非长度为0,否则永远不会执行,在这种情况下,它将是一个无限循环。 – mhodges