将长HTML元素转换为字符串

时间:2016-12-11 10:30:15

标签: javascript html

如何转换整个HTML元素:

<div class="g">
    <!--m-->
    <div class="rc" data-hveid="95" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQFQhfKAEwCg">
        <h3 class="r">
            <a href="https://en.wikipedia.org/wiki/Something_(Beatles_song)" onmousedown="return rwt(this,'','','','11','AFQjCNGXYioeoJweF-wB1uZUU1NGNI_A3Q','','0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQFghgMAo','','',event)" target="_blank">
                Something (Beatles song) - Wikipedia
            </a>
        </h3>
        <div class="s">
            <div>
                <div class="f kv _SWb" style="white-space:nowrap">
                    <cite class="_Rm">
                        https://en.wikipedia.org/wiki/<b>Something</b>_(Beatles_song)
                    </cite>
                    <div class="action-menu ab_ctl">
                        <a class="_Fmb ab_button" href="#" id="am-b10" aria-label="Result details" aria-expanded="false" aria-haspopup="true" role="button" jsaction="m.tdd;keydown:m.hbke;keypress:m.mskpe" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQ7B0IYTAK">
                            <span class="mn-dwn-arw"></span>
                        </a>
                        <div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue" data-ved="0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQqR8IYjAK">
                            <ol>
                                <li class="action-menu-item ab_dropdownitem" role="menuitem">
                                    <a class="fl" href="https://webcache.googleusercontent.com/search?q=cache:CRMWFEe8upUJ:https://en.wikipedia.org/wiki/Something_(Beatles_song)+&amp;cd=11&amp;hl=en&amp;ct=clnk&amp;gl=us" onmousedown="return rwt(this,'','','','11','AFQjCNEbZZpTsVP_Rxv695zfNmReizCs7Q','','0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQIAhjMAo','','',event)" target="_blank">
                                        Cached
                                    </a>
                                </li>
                                <li class="action-menu-item ab_dropdownitem" role="menuitem">
                                    <a class="fl" href="/search?newwindow=1&amp;q=related:https://en.wikipedia.org/wiki/Something_(Beatles_song)+something&amp;tbo=1&amp;sa=X&amp;ved=0ahUKEwjWiK-O9-vQAhUH7hoKHcAmBPwQHwhkMAo">
                                        Similar
                                    </a>
                                </li>
                            </ol>
                        </div>
                    </div>
                </div>
                <span class="st">
                    "<em>Something</em>" is a song by the Beatles, written by George Harrison and released on the band's 1969 album Abbey Road. It was also issued on a double A-sided&nbsp;...
                </span>
            </div>
        </div>
    </div>
    <!--n-->
</div>

到Javascript字符串?

2 个答案:

答案 0 :(得分:2)

您可以使用mainloop或类似内容获取元素,然后使用from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.lang import Builder from kivy.base import EventLoop Builder.load_string(''' <MyWidget>: Button: text: 'test' on_press: print('doing my job') ''') # https://github.com/kivy/kivy/blob/master/kivy/core/window/window_sdl2.py#L630 def mainloop(self): # replaced while with if if not EventLoop.quit and EventLoop.status == 'started': try: self._mainloop() except EventLoop.BaseException as inst: # use exception manager first r = EventLoop.ExceptionManager.handle_exception(inst) if r == EventLoop.ExceptionManager.RAISE: EventLoop.stopTouchApp() raise else: pass class MyWidget(BoxLayout): pass if __name__ == '__main__': from kivy.base import runTouchApp runTouchApp(MyWidget(), slave=True) # monkey patch EventLoop.window.mainloop = mainloop while True: EventLoop.window.mainloop(EventLoop.window) print('do the other stuff') if EventLoop.quit: break 获取其HTML(包括元素自带标记):

querySelector

假设您需要的元素是具有类outerHTML的文档中的第一个元素。您必须根据需要调整选择器,您可以使用任何有效的CSS选择器。请注意,如果您只需要搜索文档的一部分,那么alert(document.querySelector(".g").outerHTML); 也可用于单个元素(而不是整个文档),并且有一个表单可以返回 all <的列表/ em>匹配(g),如果你需要循环。

示例:

&#13;
&#13;
querySelector
&#13;
querySelectorAll
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这可能是一种方式

  

var str = document.getElementsByClassName('g')[0] .innerHTML;