【问题标题】:Uncaught TypeError: Cannot read properties of undefined (reading 'textContent')未捕获的类型错误:无法读取未定义的属性(读取“文本内容”)
【发布时间】:2021-12-22 13:32:49
【问题描述】:

我正在尝试从表中获取值,但出现此错误Uncaught TypeError: Cannot read properties of undefined (reading 'textContent') 有人可以帮忙解决吗?

js

 const cards = document.getElementById('cards')
 const templateCarrito = document.getElementById('template-carrito').content

 const fragment = document.createDocumentFragment()
 let carrito = {}

 cards.addEventListener('click', e => {
     addCarrito(e)
 })

const pintarCards = toolList => {
                toolList.forEach(producto => {
                   
                    templateCard.querySelectorAll('td')[0].textContent = producto.tbl_herramienta_ID
                    templateCard.querySelectorAll('td')[1].textContent = producto.tbl_herramienta_NOMBRE
                    templateCard.querySelectorAll('td')[2].textContent = producto.tbl_herramienta_DESCRIPCION
                    templateCard.querySelector('.btn-success').dataset.id = producto.tbl_herramienta_ID
                    const clone = templateCard.cloneNode(true)
                    fragment.appendChild(clone)
                })
                cards.appendChild(fragment)
            }

            // agregar al arrito
            const addCarrito = e => {
                if (e.target.classList.contains('btn-success')) {
                    console.log(e.target.dataset.id)
                    setCarrito(e.target.parentElement)
                }
                e.stopPropagation()
            }

 const setCarrito = objeto => {
                
                const producto = {
                    id: objeto.querySelector('.btn-success').dataset.id,
                    nombre: objeto.querySelectorAll('td')[1].textContent,
                    descripcion: objeto.querySelectorAll('td')[2].textContent,
                    cantidad: 1
                }
                console.log(producto)
   
            }

nombre: objeto.querySelectorAll('td')[1].textContent, descripcion: objeto.querySelectorAll('td')[2].textContent, 出现错误

【问题讨论】:

    标签: javascript arrays typescript text html-table


    【解决方案1】:

    templateCard.querySelectorAll('td') 似乎没有给您预期的结果,因此对.textContent 的调用失败(三个元素之一似乎是null)。您可以尝试记录该选择器的结果以查看结果元素是什么。

    【讨论】:

    • templateCard.querySelectorAll('td') 显示结果,但在尝试获取这些数据时出现错误。我使用香草 javascript。我还能用什么方法?
    • 是否显示三个结果?另外:请注意,浏览器将异步评估您的 console.log() 。这会导致混乱。 IE。确保在运行此代码时存在这三个 <td/> 节点。它们可能会在以后添加,这会破坏您的逻辑。
    【解决方案2】:

    这是我的html

             <table class="table table-bordered" width="100%">
                            <thead>
                                <tr>
                                    <th scope=" col">#</th>
                                    <th scope="col">Nombre</th>
                                    <th scope="col">Descripción</th>
                                    <th scope="col">Acción</th>
                                </tr>
                            </thead>
                           <tbody id="cards">
    
                          </tbody>
    
                </table>
                        <template id="template-card">
                            <tr>
                                <td></td>
                                <td id="nombre"></td>
                                <td></td>
                                <td><button class="btn btn-success"><i 
                         class="fas fa-plus"></i></button></td>
                            </tr>
                   </template>
    

    【讨论】:

    • 这是您问题的一部分,因此不应作为答案。编辑您的问题以包含此部分。
    猜你喜欢
    • 2021-12-22
    • 2021-12-25
    • 2021-11-24
    • 2021-10-31
    • 2021-11-07
    • 2022-01-17
    • 2023-03-13
    • 2022-01-01
    • 2022-01-10
    相关资源
    最近更新 更多