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

标签 javascript python html brython

所以我有以下困境:

我正在使用 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/

相关文章:

javascript - 更改链接悬停时的另一张图像

javascript - cytoscape.js 布局复合节点

python - 使用 python TVTK 或 MayaVi 探测/采样/插值 VTK 数据

html - CSS 将图像与输入对齐

javascript - Fullcalendar 不能在 Windows 机器上的 FF 中工作

javascript - JS - 对象包含数组名称

Python 类实例是重复的属性

python - Linux - IPython 中的换行符

css - 如何让HTML5标记元素在IE中正常显示

javascript - 如果复选框的值已经在 mysql 中,如何显示选中的复选框?