Показать сообщение отдельно
  #5 (permalink)  
Старый 20.01.2023, 20:19
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068

DVV,
<!DOCTYPE html>
<html>
<head>
    <title>Untitled</title>
    <meta charset="utf-8">
</head>
<body>
    <div id="linkContainer">
        <a data-href="https://link1" class="js-additional-link">Ссылка 1</a>
        <a data-href="https://link2" class="js-additional-link">Ссылка 2</a>
    </div>
    <input type="text" id="text" value="есть такая">
    <input type="text" id="link" value="https://link1">
    <button id="add">Добавить</button>
    <script>
        const btnAdd = document.getElementById('add');
        const linkContainer = document.getElementById('linkContainer');
        const txt = document.getElementById('text');
        const lnk = document.getElementById('link');

        function checLinksForMatches() {
            const boxLinkNames = document.querySelectorAll('.js-additional-link');
            const text = txt.value;
            const link = lnk.value;
            if(!text || !link) return;
            let add = true;
            for (let mainLink of boxLinkNames) {
                if (mainLink.dataset.href === link) {
                    console.log('равна');
                    add = false;
                    break;
                }
            }
            if (add) {
                console.log('Не равна');
                const newLink = document.createElement('a');
                newLink.dataset.href = link;
                newLink.textContent = text;
                newLink.classList.add('js-additional-link');
                linkContainer.append(newLink);
                txt.value = '';
                lnk.value = '';
            }

        }
        btnAdd.addEventListener('click', checLinksForMatches);
    </script>
</body>
</html>
Ответить с цитированием