使用JavaScript和PHP获取href属性的值


Using JavaScript and PHP for the value of a href attribute

在下面的代码中,我想将字符串#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
  )
});