简体   繁体   English

如何使用CSS或jQuery选择器更改命名div中的嵌套div以显示为display:inline?

[英]How to change nested divs within a named div to display as display:inline using CSS or jQuery selectors?

I'm having an issue where I need to be able to change a series of divs to use “display: inline” based on if they are nested within a particular div, however I cannot determine how to select all the sub-divs. 我遇到一个问题,我需要根据其是否嵌套在特定的div中来更改一系列div以使用“ display:inline”,但是我无法确定如何选择所有子div。 Maybe there is a simple CSS way to accomplish this, but I will describe the problem in more detail. 也许有一种简单的CSS方式可以完成此任务,但是我将更详细地描述问题。

I have a web app that used a control suite and I do not have programmatic access to change the classes/structure that the controls use, however in the end they output HTML, JavaScript, etc. and make their way onto the DOM. 我有一个使用控件套件的Web应用程序,但是我没有编程访问权限来更改控件使用的类/结构,但是最后它们输出HTML,JavaScript等,并进入DOM。 The suite wraps the control in a div that is interpreted as a block div (since no display:value is specified) which causes an issue in situations where an image or icon was displayed next to the control , eg a help icon, since divs are rendered by default as block instead of in-line. 该套件将控件包装在一个div中,该div被解释为一个块div(因为未指定display:value),这导致在控件旁边显示图像或图标(例如帮助图标)的情况下出现问题,因为div是默认情况下呈现为块而不是嵌入式。 The rest of the site needs to still treat the divs as block. 该网站的其余部分仍需要将div视为块。

Is there any way to get the added divs to add style="display: inline;" 有没有办法让添加的div添加style =“ display:inline;” to all the items it tries to wrap either through jQuery or CSS? 它试图通过jQuery或CSS包装的所有项目?

In the example below, all the divs within/beneath ctl00_ContentPlaceHolder1_Area generally need to be changed to have display: inline, however more specifically divs that start with ctl00_ctl00_ContentPlaceHolder1_* and are within the div named ctl00_ContentPlaceHolder1_Area. 在下面的示例中,通常需要更改ctl00_ContentPlaceHolder1_Area内/下的所有div才能显示:内联,但是更具体地说,以ctl00_ctl00_ContentPlaceHolder1_ *开头且位于名为ctl00_ContentPlaceHolder1_Area的div中的div。

<div id="ctl00_ContentPlaceHolder1_Area"><div id="ctl00_ctl00_ContentPlaceHolder1_TextBox1Panel">
        <input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" onchange="javascript:setTimeout('WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$TextBox1", "", true, "", "", false, true))', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;javascript:$radIE.keyPress(event);" id="ctl00_ContentPlaceHolder1_TextBox1" class="RadInputMgr_Office2007 RadInput_Enabled_Office2007" onmouseover="javascript:$radIE.mouseOver(event);" onmouseout="javascript:$radIE.mouseOut(event);" onblur="javascript:$radIE.blur(event);" onfocus="javascript:$radIE.focus(event);" />
    </div> <img src="icon.png" alt="Small Image Icon"></div>

If you have a selector you can use (ideally a class name, or something else), jQuery will let you do something like 如果您有一个可以使用的选择器(最好是一个类名或其他名称),jQuery将使您执行类似的操作

jQuery('.DIV_CLASS').each( function() { jQuery(this).css('display','inline') } )

Or, assuming they are all part of another div, css: 或者,假设它们都属于另一个div,css:

div.container_div div { display: inline; }

Try: 尝试:

#ctl00_ContentPlaceHolder1_Area div[id^="ctl00_ctl00_ContentPlaceHolder1_"] {
    display: inline !important
}

If that works, see if it works without !important , which is bad practise. 如果可行,请查看它是否没有!important ,这是不好的做法。

Demo: http://jsfiddle.net/thirtydot/RGAm8/ 演示: http//jsfiddle.net/thirtydot/RGAm8/

If you don't have any other divs within the container, you could use the following css: 如果容器中没有其他div,则可以使用以下CSS:

#ctl00_ContentPlaceHolder1_Area div {
    display: inline;
}

I would add a class to the parent div and then use css like this 我会向父div添加一个类,然后像这样使用CSS

div.parent > div { display:inline; }

This styles only the nested divs that are children of the parent div. 这将仅设置父div子级的嵌套div的样式。 In other words div 2 will be displayed inline, but div 3 will not. 换句话说,div 2将内联显示,但div 3将不会内联显示。

<div class="parent" id="1">
  <div id="2">
    <div id="3"></div>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM