【发布时间】:2021-05-23 18:27:49
【问题描述】:
我正在尝试使用 js DOM 进行推荐,但我的 innerHtml 出现错误。
我正在尝试以下代码::
import React from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faQuoteLeft, faQuoteRight } from "@fortawesome/free-solid-svg-icons";
const testimonials = [
{
name: "Miyah Myles",
position: "Marketing",
photo: "https://randomuser.me/api/portraits/women/46.jpg",
text: `It is a long established fact that a reader will be distracted by
the readable content of a page when looking at its layout. The point
of using Lorem Ipsum is that it has a more-or-less normal
distribution of letters, as opposed to using 'Content here, content
here', making it look like readable English. Many desktop publishing
packages and web page editors now use Lorem Ipsum as their default
model text, and a search`,
},
{
name: "Sheikh",
position: "Marketing",
photo: "https://randomuser.me/api/portraits/women/46.jpg",
text: `It is a long established fact that a reader will be distracted by
the readable content of a page when looking at its layout. The point
of using Lorem Ipsum is that it has a more-or-less normal
distribution of letters, as opposed to using 'Content here, content
here', making it look like readable English. Many desktop publishing
packages and web page editors now use Lorem Ipsum as their default
model text, and a search`,
},
];
const testimonialContainer = document.querySelector(".testimonials-container");
const testimonial = document.querySelector(".testimonial");
const userImage = document.querySelector(".user-image");
const username = document.querySelector(".username");
const role = document.querySelector(".role");
let idx = 1;
function updateTestimonial() {
const { name, position, photo, text } = testimonials[idx];
testimonial.innerHTML = text;
userImage.src = photo;
username.innerHTML = name;
role.innerHTML = position;
idx++;
if (idx > testimonials.length - 1) {
idx = 0;
}
}
setInterval(updateTestimonial, 10000);
const Testimonial = () => {
return (
<div className="container mt-5 custom_testi">
<div className="row">
<div className="testimonial-container">
<div className="progress-bar"></div>
<span>
<FontAwesomeIcon
icon={faQuoteRight}
className="fa-quote fa-quote-right"
/>
</span>
<span>
<FontAwesomeIcon
icon={faQuoteLeft}
className="fa-quote fa-quote-left"
/>
</span>
<p className="testimonial">
It is a long established fact that a reader will be distracted by
the readable content of a page when looking at its layout. The point
of using Lorem Ipsum is that it has a more-or-less normal
distribution of letters, as opposed to using 'Content here, content
here', making it look like readable English. Many desktop publishing
packages and web page editors now use Lorem Ipsum as their default
model text, and a search
</p>
<div className="user">
<img
src="https://randomuser.me/api/portraits/women/46.jpg"
alt=""
className="user-image"
/>
<div className="user-details">
<h4 className="username">Miyah Myles</h4>
<p className="role">Marketing</p>
</div>
</div>
</div>
</div>
</div>
);
};
export default Testimonial;
第一个是来自我的Testimonial 函数的静态数据,之后我有来自testimonials 函数的动态数据,但是当我想显示输出时,显示如下错误:
TypeError: Cannot set property 'innerHTML' of null
我尝试了很多次,但显示相同的错误。
请给点建议。
【问题讨论】:
-
除非我弄错了
className不是一个有效的 HTML 属性,至少它不常见。尝试用class替换它,看看是否能得到更好的结果。 -
这是我的 reactJs 项目 react 支持
className,我觉得没问题 -
跳过了 React 的东西——我的错。
-
您也可以尝试使用
id代替className 并使用document.querySelector(#testimonial)来获取元素。 -
@Poornaka 我也试过同样的问题。
标签: javascript html css reactjs dom