我有一个按钮,其位置通过jquery动态生成:
<a id="final_location"
href="/pre_config/step4"
class="proceed-btn right">Proceed To Next Step >>></a>
$('#final_location').click(function() {
location.href = this.href + '/' + escape($('#type_of_station').html()) +
'/' + escape($('.number_changer').attr("id").slice(-1));
return false;
});
这很有效但是当type_of_station中的html是两个单词时问题就出现了......我得到了这个网址:
pre_config/step4/Pizza Delivery/2
有没有办法让网址只给我这样的第一个字:
pre_config/step4/Pizza/2
也许这可以改为仅返回第一个单词?
答案 0 :(得分:1)
使用
encodeURIComponent($('#type_of_station').text().match(/^\S*/)[0])
而不是escape($('#type_of_station').html())
来获取第一个单词(以空格分隔)。
答案 1 :(得分:0)
这应该能为您提供短语中的第一个单词:
var type = $('#type_of_station').html();
var i = type.indexOf(' ');
if (i != -1) type = type.substr(0, i);
答案 2 :(得分:0)
你可以这样做:
var name = "Pizza Delivery";
var pieces = name.split(" ");
alert(pieces[0]); // Pizza
或者只是这样做:
var name = "Pizza Delivery";
name = name.replace(/\s/g, "-", name); // feel free to change the - delimiter
alert(name); // Pizza-Delivery