gpt4 book ai didi

javascript - Brython 将点击事件绑定(bind)到页面中尚未存在的 id

转载 作者:行者123 更新时间:2023-12-03 03:18:48 25 4
gpt4 key购买 nike

所以我有以下困境:

我正在使用 Brython,一切正常。我有一小段代码可以为我执行 ajax 请求,我将其添加到 header 中以绑定(bind)页面中当前元素上的所有内容。

    from browser import document, ajax

# URL Query String
qs = ''
# URL to work on
url = ''


def post_data(url, qs):
req = ajax.ajax()
# Bind the complete State to the on_post_complete function
req.bind('complete', on_post_complete)
# send a POST request to the url
req.open('POST', url, True)
req.set_header('content-type', 'application/x-www-form-urlencoded')
# send data as a dictionary
req.send(qs)


def get_data(url, qs):
req = ajax.ajax()
req.bind('complete', on_get_complete)
# Bind the complete State to the on_get_complete function
req.open('GET', url+'?'+qs, True)
req.set_header('content-type', 'application/x-www-form-urlencoded')
req.send()


def on_post_complete(req):
if req.status == 200 or req.status == 0:
# Take our response and inject it into the html div with id='main'
document["main_area"].html = req.text
else:
document["main_area"].html = "error " + req.text


def on_get_complete(req):
if req.status == 200 or req.status == 0:
# Take our response and inject it into the html div with id='main'
document["main_area"].html = req.text
else:
document["main_area"].html = "error " + req.text


def account_click(ev):
get_data("/account", qs)


def contact_link_click(ev):
get_data("/contact", qs)


def logo_link_click(ev):
get_data("/main_page", qs)


def products_link_click(ev):
get_data("/products_page", qs)


def register_link_click(ev):
get_data("/register", qs)

document['login_link'].bind('click', account_click)
document['contact_link'].bind('click', contact_link_click)
document['logo_link'].bind('click', logo_link_click)
document['register_link'].bind('click', register_link_click)

document['running_link'].bind('click', products_link_click)
document['fitness_link'].bind('click', products_link_click)
document['tennis_link'].bind('click', products_link_click)
document['football_link'].bind('click', products_link_click)
document['golf_link'].bind('click', products_link_click)

好吧,现在我更大的问题是 register_link 从一开始就不在页面中。更准确地说,只有在单击 login_link 链接后​​,register_link 才会加载到 DOM 中,之后注册链接不会执行任何操作,因为事件无法从开始吧。

现在我知道我可以通过在该页面中再次导入它来轻松绕过这个问题,但我想避免多余的导入,而且我不太确定如何执行此操作。

编辑:或者brython有没有办法等待DOM完全加载?

最佳答案

正如您所注意到的,像这样编写 account_click :

def account_click(ev):
get_data("/account", qs)
document['register_link'].active = True
document['register_link'].bind('click', register_link_click)

不起作用,因为程序在执行接下来的两行之前不会等待 get_data 完成。

解决方案是针对这种情况编写特定版本的 get_dataon_get_complete (我假设“register_link”按钮位于页面中,但最初被禁用):

def complete_register(req):
"""Called when the Ajax request after "login_link" is complete."""
if req.status == 200 or req.status == 0:
# Take our response and inject it into the html div with id='main'
document["main_area"].html = req.text
# enable "register link" button and add binding
document['register_link'].disabled = False
document['register_link'].bind('click', register_link_click)
else:
document["main_area"].html = "error " + req.text

def get_data_and_register(url, qs):
req = ajax.ajax()
req.bind('complete', complete_register)
req.open('GET', url+'?'+qs, True)
req.set_header('content-type', 'application/x-www-form-urlencoded')
req.send()

def account_click(ev):
get_data_and_register("/account", qs)

另一种选择是保留通用函数 get_dataon_get_complete,并添加可选参数回调:

def get_data(url, qs, callback=None):
req = ajax.ajax()
req.bind('complete', lambda req:on_get_complete(req, callback))
# Bind the complete State to the on_get_complete function
req.open('GET', url+'?'+qs, True)
req.set_header('content-type', 'application/x-www-form-urlencoded')
req.send()

def on_get_complete(req, callback=None):
if req.status == 200 or req.status == 0:
# Take our response and inject it into the html div with id='main'
document["main_area"].html = req.text
if callback is not None:
callback(req)
else:
document["main_area"].html = "error " + req.text

关于javascript - Brython 将点击事件绑定(bind)到页面中尚未存在的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com