kkngsm / blog

MIT License
0 stars 0 forks source link

yewのcurrent_target()について #24

Open kkngsm opened 1 year ago

kkngsm commented 1 year ago

https://github.com/yewstack/yew/issues/2572

kkngsm commented 1 year ago
use wasm_bindgen::JsCast;
use yew::prelude::*;
#[function_component]
fn App() -> Html {
    let onclick = |e: MouseEvent| {
        let target = e.current_target().unwrap();
        let element = target.dyn_ref::<web_sys::Element>().unwrap();
        let log = web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .get_element_by_id("log")
            .unwrap();
        log.set_inner_html(&format!(
            "log: tagName ={} id = {}, class = {}",
            element.tag_name(),
            element.id(),
            element.class_name()
        ));
    };
    html! {
        <>
        <div id={"a"}>
            <div id={"b"}>
                <div id={"c"} {onclick}>
                    {"Hello World"}
                </div>
            </div>
        </div>
        <div id={"log"}>{"log: empty"}</div>
        </>
    }
}

fn main() {
    yew::Renderer::<App>::new().render();
}