隐藏元素

jQuery: hide element

本文关键字:元素 隐藏      更新时间:2023-09-26

我有一个"反馈小部件",我想隐藏低分辨率屏幕。我的问题是,它注入以下代码后,<body>和没有什么我尝试我不能隐藏reformal_tab元素

<a id="reformal_tab" href="http://domain.com" onclick="Reformal.widgetOpen();return false;" onmouseover="Reformal.widgetPreload();" onmouseout="Reformal.widgetAbortPreload();"><img alt="" src="domain.com/tab.png"></a>

what i've try

<script>
        $('reformal_tab').hide();
        $('#reformal_tab').hide();
</script>

这是实际的小部件,如果可以帮助http://reformal.ru/

代码:

<script type="text/javascript">
$(function() {
    $('#reformal_tab').hide();
});
    var reformalOptions = {
        project_id: 93459,
        project_host: "domain.com",
        tab_orientation: "left",
        tab_indent: "50%",
        tab_bg_color: "#F05A00",
        tab_border_color: "#FFFFFF",
        tab_border_width: 2
    };
    (function() {
        var script = document.createElement('script');
        script.type = 'text/javascript'; script.async = true;
        script.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'media.reformal.ru/widgets/v3/reformal.js';
        document.getElementsByTagName('head')[0].appendChild(script);
    })();
</script>

你可以在这里看到它http://jsfiddle.net/PQsQq/2/

第二个调用是有效的调用。#通过id引用元素。

$('#reformal_tab').hide();

你还需要在文档准备好时调用它。

$(function() {
    $('#reformal_tab').hide();
});

为什么你不把它隐藏起来当你注入它的时候,然后你可以在更高分辨率的屏幕上显示它?

<a id="reformal_tab" style="display:none;" href="http://domain.com" ...

然后在你脚本的某个地方…

if(highResolution) {
  $("#reformal_tab").show();
}

为什么不直接删除呢?

$('#reformal_tab').remove();

如果元素是用javascript注入的,它可能是在DOM准备好后动态插入的。解决方案很简单,在你的CSS中:

#reformal_tab {display: none;}