如何在加载页面后将div添加到div?

时间:2016-10-08 21:36:47

标签: javascript jquery

如何在加载页面后将html添加到div? 这个HTML:

<div id="information-box"> </div>

这个jQuery:

$("#Step-02").find('#information-box', function () {
  for(i = 0; i < lenHotel; i++) {
    $("#Step-02").find('#information-box').html(
        // HTML
    );
  }
})

2 个答案:

答案 0 :(得分:0)

只需在页面加载时执行...有几种方法,但最简单的方法是使用jquery简写:

def doWork(q):
    while True:
        #logging.info('Thread Started')
        row=q.get()

        url = row[0]
        target_price = row[1]
        #logging.info('line 79')

        while True:
            delay=randint(5,10)
            headers = {'User-Agent': generate_user_agent()}
            print datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')+': '+'Sleeping for ' + str(delay) + ' seconds'
            #logging.info('line 81')
            eventlet.sleep(delay)
            try:
                #logging.info('line 85')
                with requests.Session() as s:
                    #logging.info('line 87')
                    with eventlet.Timeout(10, False):
                        page = s.get(url,headers=headers,proxies=proxyDict,verify=False)
                    #logging.info('line 89')
                    tree = html.fromstring(page.content)
                    #logging.info('line 91')
                    price = tree.xpath('//div[@class="a-row a-spacing-mini olpOffer"]/div[@class="a-column a-span2 olpPriceColumn"]/span[@class="a-size-large a-color-price olpOfferPrice a-text-bold"]/text()')[0]
                    title = tree.xpath('//h1/text()')[0]
                    #logging.info('line 93')
                    new_price = re.findall("[-+]?\d+[\.]?\d+[eE]?[-+]?\d*", price)[0]
                    #logging.info('line 95')
                    old_price = new_price
                    #logging.info('line 97')
                    #print price
                    print new_price
                    print title + 'Current price:' + new_price
                    if float(new_price)<float(target_price):
                        print 'Lower price found!'
                        mydriver = webdriver.Chrome()
                        send_simple_message()
                        login(mydriver)
                        print 'Old Price: ' + old_price
                        print 'New Price: ' + new_price
                    else:
                        print 'Trying again'
                q.task_done()   
            except Exception as e:
                print e
                print 'Error!'
                q.task_done()

答案 1 :(得分:0)

您可以在jQuery中使用ready方法,该方法在加载页面时触发。

$(document).ready(function() {
    $("#Step-02").find('#information-box', function () {
      for(i = 0; i < lenHotel; i++) {
        $("#Step-02").find('#information-box').html(
            // HTML
        );
      }
    })
});