亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 li 元素上添加帶有 href 的元素?

如何在 li 元素上添加帶有 href 的元素?

不負相思意 2023-03-18 17:07:48
我的網站上有一個搜索系統,它可以工作,但我無法點擊我搜索的項目。它是一個簡單的腳本,可以創建依賴于我的搜索輸入的 li 元素,但我如何才能將這些元素添加到自定義鏈接中?因為如果你不能點擊你搜索的項目那沒有任何意義......我想添加這個常量人物元素自定義鏈接。代碼:const people = [{name:'?????????? ????????'},{name:'????????'},{name:'mr.capsule'},{name:'capsule battle royale'}];const list = document.getElementById('list');function setlist (group) {    clearlist();    for (const person of group) {        const item = document.createElement("li");        item.classList.add("list-group-item");        const text = document.createTextNode(person.name);        item.appendChild(text);        list.appendChild(item);    }    if (group.length === 0) {        setnoresults();    }}function clearlist () {while (list.firstChild) {list.removeChild(list.firstChild);}}function getrelevency (value, searchTerm) {    if (value === searchTerm) {        return 2;    }else if (value.startsWith(searchTerm)) {        return 1;    }else if (value.includes(searchTerm)) {        return 0;    }}function setnoresults () {const item = document.createElement('li');        item.classList.add('list-group-item');        const text = document.createTextNode('???????? ??? ????????... ?????? ???????');        item.appendChild(text);        list.appendChild(item);}const searchInput = document.getElementById('search');    searchInput.addEventListener('input', (event) => {         let value = event.target.value;        if (value && value.trim().length > 0) {            value = value.trim().toLowerCase();            setlist(people.filter(person => {                return person.name.includes(value);            }).sort((personA, personB) => {                return getrelevency(personB.name, value) - getrelevency(personA.name, value);            }));        }else {            clearlist();        }    });
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

你應該在函數中添加它setlist。


function setlist (group) {

    clearlist();

    for (const person of group) {

        const item = document.createElement("li");

        item.classList.add("list-group-item");

        const link = document.createElement("a");

        link.href = "http://..."; // put here where the link should point

        item.appendChild(link);

        const text = document.createTextNode(person.name);

        link.appendChild(text);

        list.appendChild(item);

    }

    if (group.length === 0) {

        setnoresults();

    }

}


查看完整回答
反對 回復 2023-03-18
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

我通過這種方式獲得了解決方案,檢查搜索輸入是什么,如果是目標搜索,它將在自定義搜索上添加一個鏈接。如果陳述者正在做一切!


function setlist (group) {

    clearlist();

    for (const person of group) {

        const item = document.createElement("li");

        item.classList.add("list-group-item");

        if (person.name === "?????????? ????????") {

          const link = document.createElement("a");

        link.href = "https://google.com"; // put here where the link should point

        link.text = "?????????? ????????"

        item.appendChild(link);

    }

        const text = document.createTextNode(person.name);

        item.appendChild(text);

        list.appendChild(item);

    }

    if (group.length === 0) {

        setnoresults();

    }

}


查看完整回答
反對 回復 2023-03-18
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號