Javascript 如何使用 jQuery 将`style=display:"block"` 添加到元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3269136/
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 03:57:55  来源:igfitidea点击:

How to add `style=display:"block"` to an element using jQuery?

javascriptjqueryhtmlcss

提问by Someone

How to add style=display:"block"to an element in jQuery?

如何添加style=display:"block"到 jQuery 中的元素?

回答by Colin Brock

$("#YourElementID").css("display","block");

Edit:or as dave thieben points out in his comment below, you can do this as well:

编辑:或者正如 dave thieben 在下面的评论中指出的那样,您也可以这样做:

$("#YourElementID").css({ display: "block" });

回答by Design by Adrian

Depending on the purpose of setting the display property, you might want to take a look at

根据设置显示属性的目的,您可能需要查看

$("#yourElementID").show()

and

$("#yourElementID").hide()

回答by Mohammad

There are multiple function to do this work that wrote in bottom based on priority.

有多个函数可以根据优先级在底部编写此工作。

Set one or more CSS properties for the set of matched elements.

为一组匹配的元素设置一个或多个 CSS 属性。

$("div").css("display", "block")
// Or add multiple CSS properties
$("div").css({
  display: "block",
  color: "red",
  ...
})



Display the matched elements and is roughly equivalent to calling .css("display", "block")

显示匹配的元素,大致相当于调用.css("display", "block")

You can display element using .show()instead

您可以使用.show()代替显示元素

$("div").show()



Set one or more attributes for the set of matched elements.

为一组匹配的元素设置一个或多个属性。

If target element hasn't styleattribute, you can use this method to add inline style to element.

如果目标元素没有style属性,您可以使用此方法为元素添加内联样式。

$("div").attr("style", "display:block")
// Or add multiple CSS properties
$("div").attr("style", "display:block; color:red")



  • JavaScript

  • JavaScript

You can add specific CSS property to element using pure javascript, if you don't want to use jQuery.

如果您不想使用 jQuery,您可以使用纯 javascript向元素添加特定的 CSS 属性。

var div = document.querySelector("div");
// One property
div.style.display = "block";
// Multiple properties
div.style.cssText = "display:block; color:red"; 
// Multiple properties
div.setAttribute("style", "display:block; color:red");

回答by CodingYoshi

If you need to add multiple then you can do it like this:

如果您需要添加多个,那么您可以这样做:

$('#element').css({
    'margin-left': '5px',
    'margin-bottom': '-4px',
    //... and so on
});

As a good practice I would also put the property name between quotes to allow the dash since most styles have a dash in them. If it was 'display', then quotes are optional but if you have a dash, it will not work without the quotes. Anyways, to make it simple: always enclose them in quotes.

作为一个很好的做法,我还将属性名称放在引号之间以允许破折号,因为大多数样式中都有破折号。如果它是'display',那么引号是可选的,但如果你有一个破折号,没有引号它就无法工作。无论如何,为了简单起见:始终将它们括在引号中。