如何检测html是否包含使用jQuery的块级元素? [英] How to detect if html contains block level elements using jQuery?

查看:120
本文介绍了如何检测html是否包含使用jQuery的块级元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够确定特定元素的HTML内容是否包含块级元素。



我使用 $(el.html())。length == 0 表示它这是工作正常,但我很快就意识到,如果html包含



任何想法?



修改



好吧,我不是很清楚。 块级元素,我的意思是显示:block,无论是隐式还是显式的元素。



我想这样做的原因有点复杂,如下:



A。我正在使用内容可编辑区域,可以包含任何HTML或文本。
B.我有一个服务器端模块,在编辑模式下使用div中的各种类和元信息创建内容包装。
C.然后我使用jQuery:


  1. 将包装的内容移出它的外部inline ...否则包装器可以破坏页面的设计)

  2. 将包装器(不再是包装器)放在内容的后面,并将其用作突出显示的触发器


  3. 为了有效地做到2,我必须修正它的宽度和height,这很容易完成,但如果内容只是内联,则失败。想想,如果内容包含固定宽度的元素也会失败....



    重新思考我的问题,我想我的问题是,我怎么能确定HTML中的内容的任意部分的宽度....困难,不是?

    解决方案

    我的问题是一个不同的方法,使包装器 span 而不是 div 。这样我不需要移动它的内容,但将其留在包装器。包装器不强迫新行,这是好的,它的内容的宽度和高度。



    虽然我知道在技术上不正确放置块元素里面 span ,它似乎做了我想要实现的交叉浏览器。


    I want to be able to determine if the html contents of a particular element contains block level elements or not...

    I was using $(el.html()).length == 0 to indicate that it doesn't which was working fine, but I soon realised that if the html contains a "successful" css selector then the whole thing goes pear-shaped.

    Any ideas?

    Edit

    Okay, so I wasn't very clear. By "block level elements", I meant elements that are display:block, whether implicitly or explicitly.

    The reason I want to do this is a bit complicated, but it goes something like this:

    A. I'm working with "Content editable regions" that can contain any HTML or text. B. I have a server side module that creates wrapper around the content when in "edit" mode with various classes and meta information in the div. C. I am then using jQuery to:

    1. Move the contents of the wrapper outside of it (so that inline elements display inline... otherwise the wrapper can destroy the design of the page)
    2. Absolutely position the "wrapper" (no longer a wrapper) behind the content, and use it as a trigger for highlighting the contents and allowing a context menu to be displayed on right click.

    In order to do 2 effectively, I have to fix it's width and height, which is easily done, but fails if the contents is only inline. Thinking about it, it would fail if the contents contained fixed width elements too....

    So rethinking my question, I guess what I'm asking is how can I determine the width of an arbitary section of content in HTML.... Difficult, no?

    解决方案

    In the end I have solved my problem a different way by making the wrapper a span instead of a div. That way I don't need to move its contents outside but leave it inside the wrapper. The wrapper doesn't force new lines, which is good, and it takes on the width and height of its contents.

    Although I know it's not technically correct to place block elements inside a span, it seems to do the trick cross-browser of what I wanted to achieve.

    这篇关于如何检测html是否包含使用jQuery的块级元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆