在下面的代码中,我想将字符串#customer
替换为window.location.hash
。
echo '<th><a href="?sort-by=email&order='. $this->order .'#customer">E-Mail</a></th>';
在我的情况下,如何可能将JavaScript与PHP混合?
这不是PHP的问题。
只是删除哈希,因为它现在是在PHP中,然后我们可以把它放在客户端,你可以在那里添加它。例如,您可以这样编写PHP:
echo '<th><a href="?sort-by=email&order='. $this->order .'">E-Mail</a></th>';
(删除哈希值)
然后在JavaScript中,我们会这样做:
Element.href = Element.href + window.location.hash;
其中Element
为<a>
标签的DOM选择器方法。
EDIT或者如果你有jQuery的话:
$("a").each(function(){
$(this).attr("href",
$(this).attr("href") + window.location.hash
)
});