简体   繁体   English

如何使用javascript变量使用jQuery隐藏元素

[英]How do I hide an element with jQuery using a javascript variable

I am having trouble hiding this page element with the id below in the html code 我在html代码中使用下面的id隐藏此页面元素时遇到问题

<table cellspacing=5 cellpadding=3>
  <tr>
    <td id="lst2"><h4>Apple Ipad for sale N70,000 (negotiable)</h4></td>
    <td><a href="unibay.php" class="button edit">EDIT</a></td>
    <td><a href="#" onclick="del('lst1');" class="button cross">DELETE</a></td>
  </tr>
  <tr id="lst2">
    <td><h4>Apple Ipad for sale N70,000 (negotiable)</h4></td>
    <td><a href="unibay.php" class="button edit">EDIT</a></td>
    <td><a href="#" value="list2" onclick="del('lst1');" class="button">DELETE</a></td>
  </tr>
</table>

I want to be able to hide the element with the selected id using jQuery called in a javascript function. 我希望能够使用javascript函数中调用的jQuery隐藏具有所选id的元素。 Below is the code but its not working: 以下是代码但不起作用:

function del(obj) {
    $(document).ready(function(){
        var tdiv = document.getElementById(obj) ;
        var sdiv = "#" + tdiv ;
        $('.button').click(function () {
            $(sdiv).fadeOut('slow');
        });
    });
}

I think you should do like this, as your obj contains a string, the id of your target, then following approach is enough. 我认为你应该这样做,因为你的obj包含一个字符串,你的目标的id ,然后跟随方法就足够了。

var sdiv = "#" + obj;
$('.button').click(function () {
   $(sdiv).fadeOut('slow');
});

Problem in your code 您的代码中存在问题

  • var tdiv = document.getElementById(obj); line return an DOM element. line返回一个DOM元素。 So if you need to use this in your code then it will look like: 因此,如果您需要在代码中使用它,那么它将如下所示:

     function del() { var tdiv = document.getElementById(obj); $('.button').click(function() { $(tdiv).fadeOut('slow'); }); } 

Here, $(tdiv) will make your tdiv element to a jQuery object. 这里, $(tdiv)将使你的tdiv元素成为一个jQuery对象。 But tdiv.fadeOut('slow') will not work. 但是tdiv.fadeOut('slow')将不起作用。

  • and you not need $(document).ready() inside your del() function. 并且你的del()函数中不需要$(document).ready() So your code will look like: 所以你的代码看起来像:

     function del(obj) { var sdiv = "#" + obj; $('.button').click(function () { $(sdiv).fadeOut('slow'); }); } 
$(function() {

    $('.button').click(function () {
        $(this).parent().parent().fadeOut('slow');
    });

});

Things wrong with your code: 您的代码出了问题:

  • document.ready shouldn't be inside of the function. document.ready不应该在函数内部。 You put functions inside of the document.ready 你把函数放在document.ready
  • You bound the click event inside a function. 您在函数内绑定了click事件。 That's not necessary as you can use this inside the click event to get the clicked element and then .parent() twice to get the tr it belongs in. (once gets you the td , again gets you the td 's parent which is tr ) 这不是必需的,因为你可以在click事件中使用this来获取被点击的元素,然后两次.parent()来获得它所属的tr 。(一旦得到你的td ,再次得到你的td的父亲是tr

In this line: 在这一行:

        var tdiv = document.getElementById(obj);

getElementById() returns the DOM element which you then assign to 'tdiv'. getElementById()返回您随后分配给'tdiv'的DOM元素。 Thus, when you concatenate it to the "#" string you are actually adding a string + a DOM object (which doesn't work). 因此,当您将它连接到“#”字符串时,实际上是在添加一个字符串+一个DOM对象(它不起作用)。

There's an easy fix. 有一个简单的解决方案。 Since you have the id of the element stored in the variable 'obj' already, you can concatenate that: 由于你已经在变量'obj'中存储了元素的id,你可以连接:

        var sdiv = "#" + obj;
        $('.button').click(function () {
            $(sdiv).fadeOut('slow');
        });

Some of the other posters have pointed out that you shouldn't put document.ready() inside of your del function, that instead it should be outside of it. 其他一些海报已经指出你不应该在你的del函数中放入document.ready(),而应该在它之外。 I disagree completely. 我完全不同意。 If you follow their suggestion, your code will break as the 'del' function will not be available in the global namespace. 如果您遵循他们的建议,您的代码将会中断,因为“del”函数将无法在全局命名空间中使用。

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

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