jquery将变量添加到href属性

时间:2013-12-05 06:12:04

标签: jquery href attr

我敢肯定,这可能是我想要的简单但我无法理解我的生活。非常感谢任何帮助。

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

5 个答案:

答案 0 :(得分:0)

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();

演示:Fiddle

答案 1 :(得分:0)

试    

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);

});

答案 2 :(得分:0)

尝试:

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);

DEMO here.

答案 3 :(得分:0)

试试这个

您也可以使用.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);
});

答案 4 :(得分:0)

.html()是一种方法,您必须将其作为方法调用,因此您必须编写

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

除了它.html() 返回元素内的所有html标记,我认为你必须使用text(),所以文字只会包含在你的网址中

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