簡體   English   中英

jQuery將變量添加到href屬性

[英]jquery adding variables to href attribute

我敢肯定,這可能是我所忽略的簡單事情,但我無法終生解決。 任何幫助是極大的贊賞。

的HTML

<h1 class="entry-title">Installation Maintenance and Detailed Inspection</h1>
<div class="ai1ec-time">
    <div class="ai1ec-label">When:</div>
    <div class="ai1ec-field-value">January 13, 2014</div>
</div>
<div class="ai1ec-location">
    <div class="ai1ec-label">Where:</div>
    <div class="ai1ec-field-value">MOXI Perth</div>
</div>
<div class="ai1ec-cost">
    <div class="ai1ec-label">Cost:</div>
    <div class="ai1ec-field-value">AU$3200</div>    
</div>
<a id="booklink" title="Training Enrolement Form" href="http://www.moxi.com.au/training-enrolement-form/">Book Now!</a>

jQuery的

<script>
jQuery(document).ready(function(){
    var bookcourse = jQuery(".entry-title").html;
    var booktime =  jQuery(".ai1ec-time .ai1ec-field-value").html;
    var booklocation =  jQuery(".ai1ec-location .ai1ec-field-value").html;
    var bookcost =  jQuery(".ai1ec-cost .ai1ec-field-value").html;
    jQuery("#booklink").attr('href',"http://www.moxi.com.au/training-enrolement-form?title="+bookcourse+"&cost="+bookcost+"&date="+booktime+"&location="+booklocation);
});
</script>

目標是使href屬性顯示為:

http://www.moxi.com.au/training-enrolement-form?title=Installation Maintenance and Detailed Inspection&cost=AU$3200&date=January 13, 2014&location=MOXI Perth

html()是方法而不是屬性,因此您需要使用.html()而不是.html

var bookcourse = jQuery(".entry-title").html();
var booktime =  jQuery(".ai1ec-time .ai1ec-field-value").html();
var booklocation =  jQuery(".ai1ec-location .ai1ec-field-value").html();
var bookcost =  jQuery(".ai1ec-cost .ai1ec-field-value").html();

演示: 小提琴

嘗試

jQuery(document).ready(function(){
var bookcourse = $(".entry-title").html();
var booktime =  $(".ai1ec-time .ai1ec-field-value").html();
var booklocation =  $(".ai1ec-location .ai1ec-field-value").html();
var bookcost =  $(".ai1ec-cost .ai1ec-field-value").html();
jQuery("#booklink").attr('href', "http://www.moxi.com.au/training-enrolement-form?title=" + bookcourse + "&cost=" + bookcost + "&date=" + booktime + "&location=" + booklocation);

});

嘗試:

var href = $("#booklink").attr("href");
if (href.substr(0, href.length - 1) == "/") {
    href = href.substr(0, href.length - 1);
}
href += "?title=" + $(".entry-title").text();
href += "?cost=" + $(".ai1ec-cost .ai1ec-field-value").text();
href += "?date=" + $(".ai1ec-time .ai1ec-field-value").text();
href += "?location=" + $(".ai1ec-location .ai1ec-field-value").text();
$("#booklink").attr("href", href);

演示在這里。

嘗試這個

您也可以使用.text()方法

$(document).ready(function()
    var bookcourse = $(".entry-title").html();
    var booktime = $(".ai1ec-time .ai1ec-field-value").html();
    var booklocation = $(".ai1ec-location .ai1ec-field-value").html();
    var bookcost = $(".ai1ec-cost .ai1ec-field-value").html();
    $("#booklink").attr('href', "http://www.moxi.com.au/training-enrolement-form?title=" + bookcourse + "&cost=" + bookcost + "&date=" + booktime + "&location=" + booklocation);
});

.html()是一個方法,必須將其作為方法調用,因此必須編寫

jQuery(".entry-title").html();

除此之外, .html() 返回元素內的所有html標記 ,我認為您必須使用text()代替,這樣文本將僅包含在您的url中

http://api.jquery.com/text/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM