[英]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
尝试
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中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.