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

发布于 2024-10-22 01:56:20 字数 1445 浏览 1 评论 0原文

我遇到一个问题,我需要能够更改一系列 div 以根据它们是否嵌套在特定 div 中使用“display: inline”,但是我无法确定如何选择所有子 div。也许有一种简单的 CSS 方法可以完成此任务,但我将更详细地描述该问题。

我有一个使用控件套件的 Web 应用程序,并且我没有编程访问权限来更改控件使用的类/结构,但最终它们输出 HTML、JavaScript 等并进入 DOM。该套件将控件包装在一个 div 中,该 div 被解释为块 div(因为未指定 display:value),这会在控件旁边显示图像或图标(例如帮助图标)的情况下导致问题,因为 div 是默认情况下呈现为块而不是内联。网站的其余部分仍然需要将 div 视为块。

有什么方法可以让添加的div添加style =“display:inline;”它尝试通过 jQuery 或 CSS 包装的所有项目?

在下面的示例中,ctl00_ContentPlaceHolder1_Area 内/之下的所有 div 通常都需要更改为具有 display: inline,但更具体地说,以 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>

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. Maybe there is a simple CSS way to accomplish this, but I will describe the problem in more detail.

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. 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 , e.g. a help icon, since divs are rendered by default as block instead of in-line. The rest of the site needs to still treat the divs as block.

Is there any way to get the added divs to add style="display: inline;" to all the items it tries to wrap either through jQuery or 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.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-10-29 01:56:20

尝试:

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

如果有效,请看看在没有 !important 的情况下它是否有效,这是不好的做法。

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

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.

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

雄赳赳气昂昂 2024-10-29 01:56:20

如果您有一个可以使用的选择器(最好是类名或其他名称),jQuery 会让您执行类似

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

Or 的操作,假设它们都是另一个 div、css 的一部分:

div.container_div div { display: inline; }

If you have a selector you can use (ideally a class name, or something else), jQuery will let you do something like

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

Or, assuming they are all part of another div, css:

div.container_div div { display: inline; }
明月夜 2024-10-29 01:56:20

如果容器内没有任何其他 div,您可以使用以下 css:

#ctl00_ContentPlaceHolder1_Area div {
    display: inline;
}

If you don't have any other divs within the container, you could use the following css:

#ctl00_ContentPlaceHolder1_Area div {
    display: inline;
}
锦爱 2024-10-29 01:56:20

我会向父 div 添加一个类,然后使用像这样的 css

div.parent > div { display:inline; }

这仅对作为父 div 的子级的嵌套 div 进行样式设置。换句话说,div 2 将内联显示,但 div 3 不会。

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

I would add a class to the parent div and then use css like this

div.parent > div { display:inline; }

This styles only the nested divs that are children of the parent div. In other words div 2 will be displayed inline, but div 3 will not.

<div class="parent" id="1">
  <div id="2">
    <div id="3"></div>
  </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文