简体   繁体   English

如何将变量附加到javascript中的类上?

[英]How do I attach my variable to my class in javascript?

So I have been stuck on this for some time now, and cant seem to get the right syntax. 因此,我已经在此问题上停留了一段时间,并且似乎无法获得正确的语法。

I have a While loop which goes through several elements successfully until it reaches some forms. 我有一个While循环,可以成功地通过几个元素,直到达到某种形式。

I'm attempting to make each form with in the while loop to have a different(unique) class id, at the moment i'm just using a simple count which is attached to the onclick and also class. 我正在尝试使while循环中的每个表单具有不同的(唯一)类ID,此刻,我仅使用附加在onclick和类上的简单计数。

The onclick then runs the JS, I'm just struggling to add the unique count to the class in the JS, hopefully my code will explain more. 然后onclick运行JS,我只是在努力为JS中的类添加唯一计数,希望我的代码能解释更多。

JS JS

function hidelike(t) {
if ($(".like").is(':visible')) {

    $(".like").hide();
    $(".unlike").show();
    $(".dislike").hide();


} else {
   $(".like").show();
   $(".unlike").hide();
   $(".dislike").show();
}
}

HTML HTML

<form class="likesubmit<?php echo $like_count;?>">
<input type="button" class="like<?php echo $like_count;?>" value="Like"  onclick="hidelike(<?php echo $like_count;?>)">   
</form>

I have defined the count already so that's not the problem I have also alerted the count in the JS and that works fine. 我已经定义了计数,所以这不是我也已在JS中通知计数的问题,并且工作正常。

Hope I explained my problem okay, thanks in advance James. 希望我能很好地解释我的问题,谢谢詹姆斯。

well as far as I see from your HTML you have no element with a class of like. 据我从您的HTML中所看到的,您还没有像like这样的元素。 if you want to target all classes of the form like1 , like2 ... , you want to select with: http://api.jquery.com/attribute-starts-with-selector/ 如果您要定位所有形式为like1like2 ...的类, like2选择: http : like2

if ($('input[class^="like"]').is(':visible')) {

or by value 或按价值

if ($('input[value="Like"]').is(':visible')) {

Both of the above 2 methods are non-ideal What you really want to do is add an extra unique class to the inputs you want to target. 上面两种方法都不理想,您真正想要做的就是向您要定位的输入添加一个额外的唯一类。

in your PHP this time: 这次在您的PHP中:

<form class="likesubmit<?php echo $like_count;?>">
<input type="button" class="like like<?php echo $like_count;?>" value="Like"  onclick="hidelike(<?php echo $like_count;?>)">   
</form>

Notice the class="like like<?php ...." . 注意class="like like<?php ...." This way, your inputs will have 2 classes: 这样,您的输入将具有2个类:

  • a normal like class 一个正常的like
  • and a unique like1 , like2 ... etc class 和一个唯一的like1like2 ... etc类

So if you just use your current JS it will work 因此,如果您仅使用当前的JS,它将起作用

UPDATE: your JS is wrong. 更新:您的JS错误。 You are selecting ALL like, dislike, etc buttons when you click any button 当您单击任何按钮时,您正在选择所有喜欢,不喜欢等按钮

what you need is: 您需要的是:

PHP (add an extra number attribute to all buttons (also to the dislike, etc)) PHP (向所有按钮添加额外的数字属性(也向不喜欢的按钮等添加))

<form class="likesubmit<?php echo $like_count;?>">
<input type="button" class="like like<?php echo $like_count;?>" number="<?php echo $like_count; ?>" value="Like"  onclick="hidelike(<?php echo $like_count;?>)">   
</form>

JS (use the number attribute) JS (使用number属性)

function hidelike(t) {
if ($(".like" + t).is(':visible')) {
    $(".like" + t).hide();
    $(".unlike" + t).show();
    $(".dislike" + t).hide();
} else {
   $(".like" + t).show();
   $(".unlike" + t).hide();
   $(".dislike" + t).show();
}
}

You could also use the data attribute here. 您也可以在此处使用data属性。

<input class="like" data-id="<?= $like_count?>" onclick="hidelike(this)" />   

Then you can extract the property if you really need it or just hide the element. 然后,如果确实需要它,则可以提取该属性,或者仅隐藏该元素。

function hideLike(elem) {
    var $elem = $(elem);
    var id = $elem.data('id');

    $elem.hide();
    $('input.like[data-id='+id+']').hide();
}

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

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