javascript 在 <span> 标签内放置 <button> 标签是正确的放置方式

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

placing <button> tag inside <span> tag is right way of placement

javascriptjqueryhtmlcss

提问by pappu_kutty

i have requirement where i need to display text and button in one box. i have placed button tag inside span which has text and button

我有需要在一个框中显示文本和按钮的要求。我已将按钮标签放置在具有文本和按钮的跨度内

  inside span tag
  +-------------------+
  | text here         |
  |                   |    
  | <button> tag here |
  +-------------------+

my question is the right approach having button inside span. as just learned span is inline element and it can have only text, images etc..

我的问题是在跨度内设置按钮的正确方法。因为刚刚学到的跨度是内联元素,它只能包含文本、图像等。

What is the difference between HTML tags <div> and <span>?

HTML 标签 <div> 和 <span> 之间有什么区别?

回答by R.A.E

You can of course use CSS to render the span as a block element, or even an inline-block if that is what you need. Note: Some browsers do not support inline-block (Yes IE, i'm looking at you)

您当然可以使用 CSS 将 span 渲染为块元素,如果需要,甚至可以使用内联块。注意:有些浏览器不支持内联块(是的 IE,我在看着你)

.yourSpanClass {
    display: block;
}

.yourSpanClass {
    display: inline-block;
}

Though, i'd advice you to simply use a div if you have access to the HTML.

不过,如果您可以访问 HTML,我建议您只使用 div。

Supported browsers: http://caniuse.com/inline-block

支持的浏览器: http://caniuse.com/inline-block

回答by MayTheSchwartzBeWithYou

Drop the button. Use CSS/jQuery to make the span a button. Don't forget to style :hover and use JS/jQ to do the intended action

放下按钮。使用 CSS/jQuery 使跨度成为按钮。不要忘记样式 :hover 并使用 JS/jQ 来执行预期的操作