简体   繁体   中英

copy the inner html of a dynamically generated div

I want to take the innerhtml of a dynamically created div and put it in the clipboard but I keep getting a Type error. i want to access the .shortened div but I cant do it and I need help

const results = document.createElement('div')
// call api
function result(url){
    fetch(https://api.shrtco.de/v2/shorten?url=${url})
    .then(res => res.json())
    .then(data =>{
        if (data.ok){
        var data= data
        results.innerHTML = <div class="result">
        <div class="sent">${data.result.original_link}</div>
        <div class="shortened" id="short"><div>${data.result.short_link}</div>
        <div class="copy-wrapper"></div><a href="#" class="copy" onClick= "copyLink()">Copy</a></div>
        </div>
        // create div 
        answer.appendChild(results)
    }else {
        alert('incorrect url')

    }
    mainBtn.innerHTML = "Shorten it!"
 
    })
    
   
 }

为什么需要访问innerHTML,什么时候可以把缩短的url直接放到copyLink函数参数中呢?

<div class="copy-wrapper"></div><a href="#" class="copy" onClick= "copyLink('${data.result.short_link}')">Copy</a></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM