className.remove 不工作。单击按钮时尝试删除活动
Posted
技术标签:
【中文标题】className.remove 不工作。单击按钮时尝试删除活动【英文标题】:className.remove Not Working. Trying to remove active when you click on button 【发布时间】:2021-11-06 01:03:38 【问题描述】:我正在尝试创建一个删除活动功能,但它不会注册,并且我的控制台上出现错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
除了document.queryselector
或document.queryselectorAll
之外,还有什么其他方法可以添加删除功能,因为我也尝试过它们,但仍然无法正常工作。我已经添加了下面的所有代码。
var container = document.getElementById("btn-tab");
var current = container.getElementsByClassName("active");
var press = document.getElementById(this.btn);
current.className.remove("active");
press.className += " active";
<div id="btn-tab" class="tabs">
<button id="tabnews" class="tablinks active" onclick="openFeed(this)">LATEST NEWS</button>
<button id="tabevents" class="tablinks" onclick="openFeed(this)">EVENTS</button>
<button id="tabgroup" class="tablinks" onclick="openFeed(this)">GROUP</button>
</div>
<div id="tabs-feed">
test123
</div>
主题功能
function js_swappost()
//swap_post_type is a local name for the ajax call and is onlyused in her
wp_enqueue_script ("swap_post_type", get_stylesheet_directory_uri() . "/assets/js/ajax.js", array('jquery')) ;
//ajax_var is declaring a variable that is available in the client script for accessing data about the Ajax handler
//nonce is a security featur to prevent injection of Ajax calls
wp_localize_script('swap_post_type', 'ajax_var', array('ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('swapposttype'))) ;
add_action("wp_enqueue_scripts", "js_swappost") ;
//This is the function to be called when the Ajax call is made
function swap_post_ajax()
//Get the post data type to be shown, defaults to Latest News
$type = (isset($_POST["postType"])) ? $_POST["postType"] : 'latestNews' ;
//Get the nonce security code, default to 0
$nonce =(isset($_POST["nonce"])) ? $_POST["nonce"] : 0 ;
//Check that the security code is correct
if (wp_verify_nonce($nonce, 'swapposttype'))
if ($_POST == "latestNews")
// latestnewsFeed() ;
echo '<p>test</p>';
else
if ($_POST == "events")
// eventsFeed();
echo '<p>test1</p>';
else
// groupFeed();
echo '<p>Why is this not working?</p>';
else
echo 'Security Alert' ;
wp_die() ; //Needed to stop WordPress processing normally
//This is the code that actually registers the Ajax call wp_ajax is need with the element of 'swap_post_ajax' used to map to the function declared above
add_action('wp_ajax_nopriv_swap_post_ajax', 'swap_post_ajax') ; //Register the event for logged out users
add_action('wp_ajax_swap_post_ajax', 'swap_post_ajax') ; //Register the event for logged in users
CSS
button#tabnews
position: relative;
bottom: 130px;
font-size: 40px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: center;
justify-content: space-between;
align-items: flex-start;
font-family: highvoltage-rough;
background: #17CBC5;
width: 50%;
padding: 20px;
color: white;
border-radius: 0px;
margin: 5px;
font-style: normal;
font-weight: 400;
button#tabevents
position: relative;
bottom: 130px;
font-size: 40px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: center;
justify-content: space-between;
align-items: flex-start;
font-family: highvoltage-rough;
background: #17CBC5;
width: 50%;
padding: 20px;
color: white;
border-radius: 0px;
margin: 5px;
font-style: normal;
font-weight: 400;
button#tabgroup
position: relative;
bottom: 130px;
font-size: 40px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: center;
justify-content: space-between;
align-items: flex-start;
font-family: highvoltage-rough;
background: #17CBC5;
width: 50%;
padding: 20px;
color: white;
border-radius: 0px;
margin: 5px;
font-style: normal;
font-weight: 400;
@media screen and (max-width: 768px)
.tablinks
display: flex;
justify-content: space-between;
margin-left: 5px;
margin-right: 5px;
margin-top: 20px;
max-width: 50%;
text-align: left;
height: 70px;
.active
color: blue;
.tablinks:hover
background-color: #17cbc5;
color: white;
opacity: 1;
.tablinks .active
background-color: #17cbc5;
.tablinks
text-align: center;
position: relative;
margin-top: 0;
margin-bottom: -135px;
top: -127px;
@media (max-width: 959px)
.tablinks
text-align: center;
position: relative;
margin-top: 0;
margin-bottom: -160px;
top: -158px;
@media (max-width: 650px)
.tablinks
text-align: center;
position: relative;
margin-top: 0;
margin-bottom: -170px;
top: -257px;
@media (max-width: 425px)
.tablinks
text-align: center;
position: relative;
margin-top: 0;
margin-bottom: -74px;
top: -111px;
@media (max-width: 375px)
.tablinks
text-align: center;
position: relative;
margin-top: 0;
margin-bottom: -88px;
top: -113px;
@media (max-width: 366px)
.tablinks
text-align: center;
position: relative;
margin-top: 0;
margin-bottom: -151px;
top: -166px;
【问题讨论】:
className
是以字符串形式设置元素类属性内容的getter/setter。你要找的是developer.mozilla.org/en-US/docs/Web/API/Element/classList
className
是一个字符串,请改用current.classList.remove
。
你使用的是getElementsByClassName
——它将返回一个元素列表,而不是单个元素。
会试一试谢谢:)
还是什么都没有 :( 会更深入地研究它谢谢你!
【参考方案1】:
您的问题是您试图将“getElementsByClassName”放到另一个元素上。这行不通。您可以通过以下方式选择具有该类的容器的所有子项:
var current = document.querySelectorAll('#btntab > .active');
因此,您的最终代码如下所示:
var container = document.getElementById('btn-tab');
var current = document.querySelectorAll('#btntab > .active');
var press = document.getElementById('this.btn');
current.className.remove('active');
press.classList.add('active');
【讨论】:
以上是关于className.remove 不工作。单击按钮时尝试删除活动的主要内容,如果未能解决你的问题,请参考以下文章
我想通过单击 + 按钮添加相同的文本字段,但问题是只有第一个 + 按钮工作其他 + 按钮不工作?需要帮助
v-on:在 bootstrap-vue 中单击内容时单击按钮不工作