How to change visibility of item using jQuery?

喜你入骨 提交于 2019-12-12 14:18:52

问题


My application requires that users upload a set of required documents (Upload #1, Upload #2, Upload #3). When an upload completes, it returns the requirement identifier (req_id).

The page displays a list of the requirements, and currently changes the class from "missing" to "complete" once a particular item is uploaded. However, I'd also like to change the visibility of a "delete" icon from hidden to visible once the upload is complete as well.

HTML:

<ul>
    <li class="missing" rel="1">
        <span class="link">
            <a href="#" target="_blank">Upload #1</a>
        </span>
        <span class="controls">
            <img src="download.png" class="download" <? if ($req_item['class'] == "missing") { echo 'style="visibility: hidden;"'; } ?> >        
            <img src="trash.png" class="delete" <? if ($req_item['class'] == "missing") { echo 'style="visibility: hidden;"'; } ?> > 
        </span>
    </li>
    <li class="missing" rel="2">
        <span class="link">
            <a href="#" target="_blank">Upload #2</a>
        </span>
        <span class="controls">
            <img src="download.png" class="download" <? if ($req_item['class'] == "missing") { echo 'style="visibility: hidden;"'; } ?> >        
            <img src="trash.png" class="delete" <? if ($req_item['class'] == "missing") { echo 'style="visibility: hidden;"'; } ?> > 
        </span>
    </li>
    <li class="missing" rel="3">
        <span class="link">
            <a href="#" target="_blank">Upload #3</a>
        </span>
        <span class="controls">
            <img src="download.png" class="download" <? if ($req_item['class'] == "missing") { echo 'style="visibility: hidden;"'; } ?> >        
            <img src="trash.png" class="delete" <? if ($req_item['class'] == "missing") { echo 'style="visibility: hidden;"'; } ?> > 
        </span>
    </li>
</ul>

Javascript:

function stopUpload(success, req_id){

    if (success == 1){
        $('#upload_result').html('<span class="msg">Success!<\/span>');
        $("ul li[rel=" + req_id + "]").removeClass().addClass('complete')
        $("ul li[rel=" + req_id + "]").child('.controls').child('.delete').css('visibility','visible')
    }
    else {
        $('#upload_result', window.parent.document).html(
        '<span class="emsg">Error!<\/span>');
    }
    $('#upload_progress').hide();
    return true;

}

回答1:


To use jQuery, replace

$("ul li[rel=" + req_id + "]").removeClass().addClass('complete')
$("ul li[rel=" + req_id + "]").child('.controls').child('.delete').css('visibility','visible')

By:

$("ul li[rel=" + req_id + "]").removeClass().addClass('complete').find('.controls .delete').show();

However, you should use CSS for this. So keep your original jQuery and add this to CSS:

ul li .controls .delete {
    display: block;
}
ul li.complete .controls .delete {
    display: none;
}



回答2:


You want to change

$("ul li[rel=" + req_id + "]").child('.controls').child('.delete').css('visibility','visible')

to

$("ul li[rel=" + req_id + "] .controls .delete").show()

Should work nicely.

Rather than visibility, I'd tend to use the display property - display:none; or display:block; or display:inline;



来源:https://stackoverflow.com/questions/8548506/how-to-change-visibility-of-item-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!