简体   繁体   English

使用jquery同时更改情侣网站上的文本字符串

[英]change text string on couple website at the same time using jquery

22I am preparing a website which will contain prices of products on couple pages. 22我正准备一个网站,其中包含几页的产品价格。 Sometimes the same products are on couple of pages (eg on the main page and the specific product page). 有时相同的产品在几页上(例如在主页面和特定产品页面上)。 What I'm trying to achieve is to have ability of using any sort of spreadsheet or any other type of document (another perhaps) to control prices of all items across the whole website. 我想要实现的是能够使用任何类型的电子表格或任何其他类型的文档(可能是另一种)来控制整个网站上所有项目的价格。 I believe every price must be indexed somehow so we know that in with id="product1" will be the correct price and different than in id="product2". 我相信每个价格都必须以某种方式编入索引,因此我们知道在id =“product1”中将是正确的价格并且与id =“product2”不同。 Currently I have the example code here: 目前我在这里有示例代码:

<h3>Product 1</h3>
<div class="price">
    <span id="product1">£55 per day</span>
</div>

<h3>Product 2</h3>
<div class="price">
    <span id="product2">£20 per day</span>
</div>

etc...

Sorry for rather a 'question type' topic than the 'case type', but I was trying to find the solution already. 对不起'问题类型'主题而不是'案例类型',但我试图找到解决方案。 I know it can be done in php, but I have no idea about php unfortunately. 我知道它可以在php中完成,但不幸的是我不知道php。 So anything in html / javascript will be handy. 所以html / javascript中的任何内容都会很方便。 Thnx a lot for any help/advice. 对于任何帮助/建议,Thnx很多。

I would recommend storing the data in either a database or an xml file to be read by the website. 我建议将数据存储在数据库或xml文件中以供网站阅读。 That way it's a "change once" situation. 这就是“改变一次”的情况。 However, the scope of what needs to be done is beyond what you'd find in a simple answer here. 但是,需要完成的工作范围超出了您在此处的简单答案中所能找到的范围。

Edit: Jquery is a client side language, which means that it will only change what's currently exposed to the client at that time. 编辑:Jquery是一种客户端语言,这意味着它只会改变当时公开给客户端的内容。 It does have the ability to read from an xml file, and use that data to populate the display. 它确实能够从xml文件中读取,并使用该数据填充显示。 But that data does need to be stored externally for it to affect more than one page at the same time. 但是,该数据确实需要存储在外部,以便同时影响多个页面。

use JSON, not XML It's not 2003. Your jquery would be: 使用JSON,而不是XML它不是2003.你的jquery将是:

var prices = $.get("prices.json")
var product;
$("h3").each.( function()
{
    product = $(this).html();
    $(this).next().children("span").html(prices[product]);
});

Assuming you have no other H3 's on your pages, otherwise give each product ID 'h3' a class a la: 假设你的页面上没有其他H3 ,否则给每个产品ID'h3'一个类a la:

<h3 class="products">Product 1</h3>

and use $(".products") instead of $("h3") . 并使用$(".products")代替$("h3")

You could also use a selector to pull the <div> 's by class, and fetch the child <span> 's id . 您还可以使用选择器按类拉出<div> ,并获取子<span>id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM