假設標籤是從谷歌地圖的嵌入鏈接:如何使用jQuery?從文本輸入字段中發佈的<iframe>標籤中獲取「src」屬性?
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3025.306387423317!2d-74.04668908414591!3d40.68924937933434!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c25090129c363d%3A0x40c6a5770d25022b!2sStatue+of+Liberty+National+Monument!5e0!3m2!1sen!2sin!4v1495685672306" width="800" height="600" frameborder="0" style="border:0" allowfullscreen></iframe>
而且我也有一個按鈕,粘貼到該文本輸入字段:
<input type="text" id="embed_link" name="embed_link">
<button class="save_button" data-button="location">Save</button>
然後我可以得到iframe標籤的價值使用jQuery像這樣:
$(document).ready(function() {
$(".save_button").on('click', function() {
var embed_link = $("#embed_link").val();
});
});
我怎麼會那麼您可以通過embed_link
變量「src」屬性?
這是我已經試過:
var embed_src = embed_link.attr('src');
or
var embed_src = $("#embed_link").val().attr('src');
//(doesn't work. I keep getting this error:
Uncaught TypeError: $(...).val(...).attr is not a function.
It probably has to be an iframe on the page for this to work I guess?)
下面是同一
感謝。接受第一個回覆的答案。 – Aj334
謝謝@ Aj334很高興幫助 –