Javascript 在javascript中将样式显示从无更改为阻止?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12174133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:18:33  来源:igfitidea点击:

Changing the style display from none to block in javascript?

javascripthtmljquery

提问by M Sach

Here is html code snippet

这是html代码片段

<li style="opacity: 1;">
    <a id="LinkDisplay" class="optionsDropDown" style="color:#FF0000;display:none" href="javascript:showThisLink('LinkId');">
</li>

Here is jquery function which is being called at on load

这是在加载时调用的 jquery 函数

$(function () {
    $.ajax({
        url: url,
        dataType: 'json',
        data: '',
        type: 'POST',
        success: function (data) {
            alert("Test");
            document.getElementById("LinkDisplay").style.diplay="block"; // line 1
            // after this line execution i should see the link as i have
            // changed the link display from none to block but it is still invisible
        });
    });
}

After line 1 execution , I am not sure why my link is not becoming visible?

第 1 行执行后,我不确定为什么我的链接不可见?

采纳答案by Henrik Karlsson

Since you are using jQuery you could just write

由于您使用的是 jQuery,因此您可以编写

$("#elemId").show()

回答by Neji

you have not changed the display property in your code

您没有更改代码中的显示属性

 document.getElementById("LinkDisplay").style.display="block"

insert this code into your function after line1

将此代码插入到第 1 行之后的函数中

回答by Prasad DLV

The problem is the spelling of displayin the line:

问题是显示在行中的拼写:

document.getElementById("LinkDisplay").style.display="block";

回答by whostolemyhat

The parent liis set to opacity: 0, which makes it transparent.

父项li设置为opacity: 0,这使其透明。

You'll need to update its' opacity to 1 to make it visible.

您需要将其不透明度更新为 1 以使其可见。

Replace this:

替换这个:

document.getElementById("LinkDisplay").style.color = "#FF0000";

with this:

有了这个:

$('#LinkDisplay').show().parent('li').css({opacity: 1});

The second line is jQuery (since you're using jQuery already and it's easier to find the parent node) - it's finding the LinkDisplay link and changing the display: noneto display: block, then alters the parent li's opacity to make it visible.

第二行是 jQuery(因为您已经在使用 jQuery 并且更容易找到父节点) - 它找到 LinkDisplay 链接并将 更改display: nonedisplay: block,然后更改父节点li的不透明度以使其可见。

Working jsFiddle

工作 jsFiddle

回答by Brombomb

It also looks like you're already using jquery so you can simplify a bit by using the $ selector syntax:

看起来您已经在使用 jquery,因此您可以使用 $ 选择器语法进行一些简化:

$('#LinkDisplay').css('display', 'block')

You could also use the jQuery showmethod to shorten the first part like so:

您还可以使用jQuery show方法来缩短第一部分,如下所示:

$('#LinkDisplay').show()

The jQuery selector can find elements by ID or classes using the #for id's and .for classes. The jQuery cssmethod allows you to both get and set properties using a variety of methods. And the jQuery parentmethod quickly allows you to traverse upwards from an element in the DOM to find other tags.

jQuery 选择器可以使用#for id 和.for classes按 ID 或类查找元素。该jQuery的CSS的方法可以让你使用各种方法get和set属性。并且jQuery 的 parent方法可以快速让你从 DOM 中的一个元素向上遍历以查找其他标签。

回答by dan

Using jQuery:

使用jQuery:

$('#LinkDisplay').css('display','block');
$('#LinkDisplay').parent().css('opacity','1');