我知道这是一个老问题,但我偶然发现了这篇帖子,寻找使用CSS创建超链接的解决方案,最终自己制作了一个,可能对像我一样偶然发现这个问题的人有所帮助:
这里有一个名为“linker();”的php函数,它启用了一个虚假的CSS属性
connect: 'url.com';
用于定义#id项目。只需让php在您认为值得链接的每个HTML项目上调用此功能即可。输入是.css文件作为字符串,使用:
$style_cont = file_get_contents($style_path);
以及相应项目的#id。这就是整个过程:
function linker($style_cont, $id_html){
if (strpos($style_cont,'connect:') !== false) {
$url;
$id_final;
$id_outer = '#'.$id_html;
$id_loc = strpos($style_cont,$id_outer);
$connect_loc = strpos($style_cont,'connect:', $id_loc);
$next_single_quote = stripos($style_cont,"'", $connect_loc);
$next_double_quote = stripos($style_cont,'"', $connect_loc);
if($connect_loc < $next_single_quote)
{
$link_start = $next_single_quote +1;
$last_single_quote = stripos($style_cont, "'", $link_start);
$link_end = $last_single_quote;
$link_size = $link_end - $link_start;
$url = substr($style_cont, $link_start, $link_size);
}
else
{
$link_start = $next_double_quote +1;
$last_double_quote = stripos($style_cont, '"', $link_start);
$link_end = $last_double_quote;
$link_size = $link_end - $link_start;
$url = substr($style_cont, $link_start, $link_size);
}
$connect_loc_rev = (strlen($style_cont) - $connect_loc) * -1;
$id_start = strrpos($style_cont, '#', $connect_loc_rev);
$id_end = strpos($style_cont,'{', $id_start);
$id_size = $id_end - $id_start;
$id_raw = substr($style_cont, $id_start, $id_size);
$id_clean = rtrim($id_raw);
if (strpos($url,'http://') !== false)
{
$url_clean = $url;
}
else
{
$url_clean = 'http://'.$url;
};
if($id_clean[0] == '#')
{
$id_final = $id_clean;
if($id_outer == $id_final)
{
echo '<a href="';
echo $url_clean;
echo '" target="_blank">';
};
};
};
};
使用像.wrap()或getelementbyID()这样的命令可能可以改进/缩短此代码,因为它只生成<a href='blah'>
部分,但是由于没有开放子句,</a>
会自动消失,所以只要在任何地方添加它们就可以正常工作 :D