javascript - 向页面添加新元素
问题描述
所以,我对反应很陌生,所以我仍然不太确定我在做什么。我有一个使用纯 HTML、JQuery 和 CSS 的项目,但我正在尝试将其转换为响应。我有一个带有复选框的页面,当单击它们时,会在其中添加一个带有链接的 div,如果未选中,则会删除该 div:
if(element.checked != false) {
$('.links').append('<div id="social">\
<p>\
<a href = "https://instagram.com/" id="instagram">Instagram</a>\n\
<button type="button" id="instagram" onclick="removeInstagram()">-</button>\
</p>\
<p>\
<a href= "https://facebook.com/" id="facebook">Facebook</a>\n\
<button type="button" id="facebook" onclick="removeFacebook()">-</button>\
</p>\
<p>\
<a href= "https://twitter.com/" id="twitter">Twitter</a>\n\
<button type="button" id="twitter" onclick="removeTwitter()">-</button>\
</p>\
<p>\
<a href= "https://youtube.com/" id="youtube">Youtube</a>\n\
<button type="button" id="youtube" onclick="removeYoutube()">-</button>\
</p>\
</div>');
}
else if(element.checked != true) {
$("#social").remove();
}
我正在尝试将其转换为组件内的 React,但我不确定我是否做得对。我有链接到我的复选框的功能,但我不知道单击时如何添加/删除链接:
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { findDOMNode } from "react-dom";
import $ from "jquery";
export default class LoginPage extends Component {
render() {
return (
<div id="container">
<section className="presets">
<div>
<label for="item1">Social Media:</label>
<input
type="checkbox"
name="item1"
id="item1"
onChange={this.socialMedia.bind(this)}
/>
</div>
<div>
<label for="item2">Tech:</label>
<input
type="checkbox"
name="item2"
id="item2"
onChange="toggleTech(this)"
/>
</div>
<div>
<label for="item3">Sports:</label>
<input
type="checkbox"
name="item3"
id="item3"
onChange="toggleSports(this)"
/>
</div>
<div>
<label for="item4">News:</label>
<input
type="checkbox"
name="item4"
id="item4"
onChange="toggleNews(this)"
/>
</div>
<div>
<label for="item5">Games:</label>
<input
type="checkbox"
name="item5"
id="item5"
onChange="toggleGames(this)"
/>
</div>
<div>
<label for="item3">School:</label>
<input
type="checkbox"
name="item6"
id="item6"
onChange="toggleSchool(this)"
/>
</div>
<div>
<button
type="button"
id="custom"
onClick="customHandler(this)"
>
Add Custom Site
</button>
</div>
</section>
<section className="links"></section>
</div>
);
}
// Social(element) {
// if (element.checked != false) {
// $(".links").append(
// '<div id="social">\
// <p>\
// <a href = "https://instagram.com/" id="instagram">Instagram</a>\n\
// <button type="button" id="instagram" onclick="removeInstagram()">-</button>\
// </p>\
// <p>\
// <a href= "https://facebook.com/" id="facebook">Facebook</a>\n\
// <button type="button" id="facebook" onclick="removeFacebook()">-</button>\
// </p>\
// <p>\
// <a href= "https://twitter.com/" id="twitter">Twitter</a>\n\
// <button type="button" id="twitter" onclick="removeTwitter()">-</button>\
// </p>\
// <p>\
// <a href= "https://youtube.com/" id="youtube">Youtube</a>\n\
// <button type="button" id="youtube" onclick="removeYoutube()">-</button>\
// </p>\
// </div>'
// );
// } else if (element.checked != true) {
// $("#social").remove();
// }
// }
}
对此的任何帮助将不胜感激,我正在为此使用 MERN 堆栈。
解决方案
用 func comp 和 useState 试试这个
export const LoginPage = () => {
const [isChecked, setIsChecked] = useState(false);
return (
<div id="container">
<section className="presets">
<div>
<label for="item1">Social Media:</label>
<input
type="checkbox"
name="item1"
id="item1"
onChange={() => setIsChecked(!isChecked)}
/>
</div>
<div>
<label for="item2">Tech:</label>
<input
type="checkbox"
name="item2"
id="item2"
onChange="toggleTech(this)"
/>
</div>
<div>
<label for="item3">Sports:</label>
<input
type="checkbox"
name="item3"
id="item3"
onChange="toggleSports(this)"
/>
</div>
<div>
<label for="item4">News:</label>
<input
type="checkbox"
name="item4"
id="item4"
onChange="toggleNews(this)"
/>
</div>
<div>
<label for="item5">Games:</label>
<input
type="checkbox"
name="item5"
id="item5"
onChange="toggleGames(this)"
/>
</div>
<div>
<label for="item3">School:</label>
<input
type="checkbox"
name="item6"
id="item6"
onChange="toggleSchool(this)"
/>
</div>
<div>
<button
type="button"
id="custom"
onClick="customHandler(this)"
>
Add Custom Site
</button>
</div>
</section>
<section className="links">
{isChecked ? <div id="social">
<p>
<a href = "https://instagram.com/" id="instagram">Instagram</a>
<button type="button" id="instagram" onclick="removeInstagram()">-</button>\
</p>
<p>
<a href= "https://facebook.com/" id="facebook">Facebook</a>
<button type="button" id="facebook" onclick="removeFacebook()">-</button>\
</p>
<p>
<a href= "https://twitter.com/" id="twitter">Twitter</a>
<button type="button" id="twitter" onclick="removeTwitter()">-</button>\
</p>
<p>
<a href= "https://youtube.com/" id="youtube">Youtube</a>
<button type="button" id="youtube" onclick="removeYoutube()">-</button>\
</p>
</div> : null }
</section>
</div>
);
}
推荐阅读
- snowflake-cloud-data-platform - 如果该列中的值为空,如何用数字 0 替换字符串列
- javascript - reactjs中的多维状态更新
- c# - 插入后显示最后一条记录c#
- authentication - 了解 ASP.NET Core Identity 中的 oauth 外部登录身份验证流程
- r - 一个参数case_when中有太多值?
- mysql - SQL:从联合表中删除计数条件
- java - Axon Config - @eventhandlers 抛出异常后的 Kafka 重试策略
- javascript - NodeJS 在需要整数的地方返回 0
- r - Shiny- 每个复选框的工具提示(基本)
- git - 硬重置后可以从 git 初始提交中检索已删除的文件吗?