如何使用JS React选择活动幻灯片和表元素?

huangapple go评论74阅读模式
英文:

How to select the active slide and a table element using JS React?

问题

Here's the translated part of your text:

我是一个网页开发的初学者作为一项练习的一部分我想修改下面链接的javascript代码
我想要使按钮元素可点击

`https://codepen.io/hexagoncircle/pen/jgGxKR?editors=0010`

const slide1Button = document.querySelector('.btn');
slide1Button.addEventListener('click', function() {
  window.open(slideData[0].url);
});

const slide2Button = document.querySelector('.btn');
slide2Button.addEventListener('click', function() {
  window.open(slideData[1].url);
});

const slideData = [
{
  index: 0,
  button: 'VISITER',
  url: 'https://www.interflora.fr/',
  src: 'https://lecoqetlecerisier.files.wordpress.com/2011/05/dsc037411.jpg'
},
{
  index: 1,
  button: 'VISITER',
  url: 'https://www.123fleurs.com/',
  src: 'http://idata.over-blog.com/1/39/34/32/Poitiers-Ici-et-La/Macro-fleur-rose.JPG'
},
{
  index: 2,
  button: 'VISITER',
  url: 'https://www.aquarelle.com/',
  src: 'http://monmondevirtuel.m.o.pic.centerblog.net/o/91d323e8.jpg'
},
{
  index: 3,
  button: 'VISITER',
  url: 'https://www.florajet.com/',
  src: 'http://www.juille.com/images/006_3.JPG'
}
];

我已经添加了这段代码

它部分工作因为我无法确定为当前幻灯片选择按钮并在单击时根据slideData表中的URL重定向

这仅选择表中的第一个URL我希望使元素具有交互性并使其跟随当前幻灯片

如果有人可以帮助我或建议我的方法

Is there anything else you would like to know or do with this text?

英文:

I am a beginner in web development. As part of an exercise I would like to modify the javascript code of the link below.
I would like to make the button elements clickable.

https://codepen.io/hexagoncircle/pen/jgGxKR?editors=0010


  const slide1Button = document.querySelector('.btn'); 
  slide1Button.addEventListener('click', function() {
  window.open(slideData[0].url);
  }); 


  const slide2Button = document.querySelector('.btn'); 
  slide2Button.addEventListener('click', function() {
  window.open(slideData[1].url);
  }); 


const slideData = [
{
  index: 0,
 
  button: 'VISITER',
  url: 'https://www.interflora.fr/',
  src: 'https://lecoqetlecerisier.files.wordpress.com/2011/05/dsc037411.jpg' },
 

{
  index: 1,

  button: 'VISITER',
  url: 'https://www.123fleurs.com/',
  src: 'http://idata.over-blog.com/1/39/34/32/Poitiers-Ici-et-La/Macro-fleur-rose.JPG' },

{
  index: 2,

  button: 'VISITER',
  url: 'https://www.aquarelle.com/',
  src: 'http://monmondevirtuel.m.o.pic.centerblog.net/o/91d323e8.jpg' },

{
  index: 3,

  button: 'VISITER',
  url: 'https://www.florajet.com/',
  src: 'http://www.juille.com/images/006_3.JPG' }
];

I have added the code

It works halfway, because I can't determine that for slide current it selects the button and on click redirects according to the url written in the slideData table.

this only selects the first url of the table i want to make the element interactive and have it follow the current slide.

If anyone can help me or advise me in my approach.

答案1

得分: 0

以下是翻译好的部分:

Slide Component -> render()

<button className="slide__action btn" onClick={() =>  window.open(url, '_blank')}>{button}</button>

Don't forget to import url prop into the Slide Component

const { src, button, headline, index, url  } = this.props.slide

Here is a working example : CodePen.

英文:

You can add an onClick event on the button in the Slide component itself.

Slide Component -> render()

<button className="slide__action btn" onClick={() =>  window.open(url, '_blank')}>{button}</button>

Don't forget to import url prop into the Slide Component

const { src, button, headline, index, url  } = this.props.slide

Here is a working example : CodePen.

huangapple
  • 本文由 发表于 2023年4月10日 21:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定