我想在單擊按鈕時使用來自輸入的值將一個 with href 附加到 Google 的 favicon 提取器。
我想要達到的結果是:
<img href="https://s2.googleusercontent.com/s2/favicons?domain=https://google.com/">
實際結果:
<img href="https://s2.googleusercontent.com/s2/favicons?domain=[object Undefined]">
https://jsfiddle.net/u5tp79a4
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="upload.js"></script>
<style>
#e-favicon {
height: 100px;
width: 100px;
}
</style>
</head>
<body>
<input type="text" id="input">
<button id="button">Get favicon</button>
<div id="e-favicon"></div>
</body>
</html>
const element = "#e-favicon";
const button = "#button";
const input = "#input";
$(document).ready(function() {
console.log("js loaded");
$(button).on('click', function () {
const favicon = "https://s2.googleusercontent.com/s2/favicons?domain=" toString($(input).val());
console.log("button click");
console.log(favicon);
$(element).append('<img href="' favicon '">');
});
});
uj5u.com熱心網友回復:
轉換為字串是導致意外結果的原因。您不需要轉換.val()為字串,因為它回傳一個字串值:
$(button).on('click', function () {
const favicon = "https://s2.googleusercontent.com/s2/favicons?domain=" $(input).val();
//...
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/435259.html
標籤:javascript jQuery
