简体   繁体   English

在 html 页面之间操作值

[英]Manipulate values ​between html pages

I'm creating a portfolio, and in that portfolio there will be several cards, and the purpose of these cards will be for them to be redirected to another page with detailed information.我正在创建一个投资组合,在该投资组合中会有几张卡片,这些卡片的目的是将它们重定向到包含详细信息的另一个页面。 And that's where the question comes in, instead of creating an html page for each card wouldn't it be advantageous to make a page only, and change the values between each card when clicked on js?这就是问题所在,而不是为每张卡片创建一个 html 页面,仅制作一个页面并在单击 js 时更改每张卡片之间的值不是有利的吗? A buddy told me that there is a rounter property of react, where it is possible to do this, but the project is simple for now, and I would like to solve this issue with js.有小伙伴告诉我react有一个rounter属性,在哪里可以做到,不过项目目前比较简单,想用js解决这个问题。 Below is a separate example (which is not part of the main project): Main page:下面是一个单独的示例(不是主项目的一部分): 主页:

<a href="/pagina2.html" id="a1">Aqui</a>
<a href="/pagina2.html" id="aa">Teste</a>

Page 2第2页

<div id="main">
        <h1 id="title">Lindo</h1>
</div>

Js JS

let a1 = document.getElementById('a1');
var aa = document.getElementById('aa');
var h1 = document.querySelector('#title');
var main = document.getElementById('main');


if (onclick == a1) {
    title.innerText = "Leandro O cachorro";
    main.style.backgroundColor = 'green';
}else if(onclick == aa){
    console.log('era pra estar vermelho');
    title.innerText = "Belle Belinha";
    main.style.backgroundColor = 'red';
}

I tried to use the if-else sentence, but it only takes the first if, even clicking on link 2我尝试使用if-else语句,但它只需要第一个if,即使点击链接2

instead of creating an html page for each card wouldn't it be advantageous to make a page only, and change the values between each card when clicked on js?而不是为每张卡创建一个 html 页面,只制作一个页面,并在单击 js 时更改每张卡之间的值不是很有利吗?

Of course, everyone loves that way.当然,每个人都喜欢这种方式。

Frankly speaking, it is possible but you can't achieve this with just what you are showing above.坦率地说,这是可能的,仅凭上面显示的内容是无法实现的。 I think you can't achieve what you want in that way.我认为你无法以这种方式实现你想要的。

So instead, I will recommend you to use react .因此,我建议您使用react In case you don't like it, take a look of alternatives below.如果您不喜欢它,请查看下面的替代方案。


If you consider react is overkill, you can try a simpler solution: static site generator .如果您认为react是多余的,您可以尝试一个更简单的解决方案: static site generator Check: Hugo , Jekyll , Eleventy , etc... These approaches help you focus on site building instead of architecture and technical building.检查: HugoJekyllEleventy等...这些方法可帮助您专注于网站建设,而不是架构和技术建设。

If you want a lighter solution and prefer to do more stuff, then pick a bundler like webpack , rollup , parcel , etc... and then apply templating libraries likes Handlebar JS , Mustache JS , Liquid JS , EJS , etc... This approach will give you more chance to do low-level stuff and super lightweight.如果您想要一个更轻的解决方案并且喜欢做更多的事情,那么选择一个像bundlerrollupparcel等这样的捆绑webpack ,然后应用像Handlebar JSMustache JSLiquid JSEJS这样的模板库......这个方法会给你更多的机会做低级的东西和超轻量级的东西。

Lastly, build everything from scratch.最后,从头开始构建一切。 yes, still possible but sure unless you know most of the things and know where to go, and what to do.是的,仍然有可能,但可以肯定的是,除非你知道大部分事情并且知道 go 的位置,以及该怎么做。

The only one trick I could think of for exchanging information between different pages while only using front-end javascript: You can link to the other page using parameters at the end: IE: myUrl + "?myParameter1=myValue1&myParameter2=myValue2"在仅使用前端 javascript 时,我能想到的在不同页面之间交换信息的唯一一个技巧:您可以使用末尾的参数链接到另一个页面:IE: myUrl + "?myParameter1=myValue1&myParameter2=myValue2"

Now on that second page you could retrieve those parameters using:现在在第二页上,您可以使用以下方法检索这些参数:

var url = new URL(window.location.href);
url.searchParams.get("myParameter1"); // "myValue1"
url.searchParams.get("myParameter2"); // "myValue2"
//...

...and depending on the parameter(s) passed, you can now display different content on the page, which can even be bookmarked or even shared via the link (as a nice sideffect). ...并且根据传递的参数,您现在可以在页面上显示不同的内容,甚至可以添加书签甚至通过链接共享(作为一个很好的副作用)。

Edit: Sorry, I might have misunderstood your question, but maybe this helps you to find an alternate solution anyway (at least you won't have to make a new html page for EVERY card, just one).编辑:抱歉,我可能误解了您的问题,但也许这有助于您找到替代解决方案(至少您不必为每张卡制作新的 html 页面,只需一张)。

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

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