getElementById神秘地返回URL

mat*_*t-p 1 javascript jquery twitter-bootstrap

我有一个函数,它将thumbnail路径作为值,等待引导模式打开,然后将thumbnail href模态内的值设置为正确的路径.

然而,当我console.logelement(el),我得到的页面即网址

Element is http://localhost:8086/project/etc/etc
Run Code Online (Sandbox Code Playgroud)

我错过了一些明显的东西或做些傻事吗?

  function addThumbnailPathToModal(thumbnailPath) {
      $('#my-modal').on('show.bs.modal', () => {
        const el = document.getElementById('thumbnail');
        console.log(`Element is ${el}`)
        el.setAttribute('href', thumbnailPath);
      })
    }

$('.something').on('click', '.open-modal', () => {
  const thumbnailPath = getThumbnailPathFromAttribute();
  addThumbnailPathToModal(thumbnailPath);
  $('#my-modal').modal();
});
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 8

调用模板字符串时,将toString调用该对象的函数.由于历史原因,A元素在这种情况下返回href.

记录值的最佳做法是避免将它们记录为字符串:

console.log(`Element:`, el)
Run Code Online (Sandbox Code Playgroud)

如果你真的需要将它们输出到字符串,outerHTML可能就是你想要的:

var str = `Element is ${el ? el.outerHTML : null}`;
Run Code Online (Sandbox Code Playgroud)

  • @ÁlvaroGonzález你也可以在Chrome中做到这一点,但没有真正的收获,只需传递几个参数 (2认同)