循环打印CSS样式在多次打印一个hrefs

时间:2021-08-30 19:43:01

I have used the search feature on this site and Googled around but have not found a definitive answer.Hoping someone may be able to point me in the right direction.

我在这个网站上使用了搜索功能,在谷歌上搜索了一下,但是没有找到一个明确的答案。希望有人能指点我正确的方向。

I am styling links in a PHP print statement as follows:

我在PHP打印语句中样式化链接如下:

print '<a href="'.$linkPath.'index.php" style="color:white">Home</a> | <a href="'.$linkPath.'scripts/login.php" style="color:white">Login</a>'. PHP_EOL;

This is working for me, however the supplied code is only a brief example and I have several more links.

这是为我工作,但是提供的代码只是一个简单的例子,我还有几个链接。

Considering the fact that this falls into the code redundancy category with the repeating of

考虑到这属于重复的代码冗余类别

style="color:white"

I am looking for another solution.

我在寻找另一种解决办法。

Could a loop be used in this situation and if so could someone give an example of how I might write it?

在这种情况下可以使用循环吗?如果有的话,有人能举个例子来说明我是怎么写的吗?

How can I add a style to my links without having to explicitly specify it for every link?

如何向链接添加样式,而不必为每个链接显式指定样式?

2 个解决方案

#1


3  

Don't use inline styles. Use CSS classes instead, like this

不要使用内联样式。使用CSS类,比如这样

print '<a href="'.$linkPath.'index.php" class="white">Home</a> | <a href="'.$linkPath.'scripts/login.php" class="white">Login</a>'. PHP_EOL;

打印< a href = "。linkPath。美元指数。php" class="white">Home | < / >登录”。PHP_EOL;

and in your CSS

和在你的CSS

.white{
     color:white;
 }

If you truly want to avoid any code redundancy, you can use CSS pseudoselectors

如果您真的想避免任何代码冗余,您可以使用CSS伪选择器

#div>a{
    color:white;
}

#2


2  

Probably, using css classes is what you are looking for. You can achieve that using class attribute in your tags, of course you have to write those class parameters using css.
CSS:

可能,使用css类是您要找的。您可以在标记中使用class属性实现这一点,当然您必须使用css编写这些类参数。CSS:

.white {
   color:white;
}

HTML:

HTML:

<a href="index.php" class="white">Home</a>

Well you said something about loops, huh? If you want to power a navigation menu by loops, the best way would be using multidimensional arrays and for loop.

你说了关于循环的事?如果你想通过循环来驱动导航菜单,最好的方法是使用多维数组和for循环。

$links = array('Home' => 'index.php', 'login' => 'login.php');
$values = array_values($links);
$keys = array_keys($links);

for($i=0; $i<count($links); $i++) {
   print('<a href="' . $values[$i] . '" class="white">' . $keys[$i] . '</a> | ');
}

foreach example(a lot easier though):

对于每个例子(虽然简单得多):

$links = array('Home' => 'index.php', 'login' => 'login.php');

foreach($links as $val => $link) {
   print('<a href="' . $link . '" class="white">' . $val . '</a> | ');
}

#1


3  

Don't use inline styles. Use CSS classes instead, like this

不要使用内联样式。使用CSS类,比如这样

print '<a href="'.$linkPath.'index.php" class="white">Home</a> | <a href="'.$linkPath.'scripts/login.php" class="white">Login</a>'. PHP_EOL;

打印< a href = "。linkPath。美元指数。php" class="white">Home | < / >登录”。PHP_EOL;

and in your CSS

和在你的CSS

.white{
     color:white;
 }

If you truly want to avoid any code redundancy, you can use CSS pseudoselectors

如果您真的想避免任何代码冗余,您可以使用CSS伪选择器

#div>a{
    color:white;
}

#2


2  

Probably, using css classes is what you are looking for. You can achieve that using class attribute in your tags, of course you have to write those class parameters using css.
CSS:

可能,使用css类是您要找的。您可以在标记中使用class属性实现这一点,当然您必须使用css编写这些类参数。CSS:

.white {
   color:white;
}

HTML:

HTML:

<a href="index.php" class="white">Home</a>

Well you said something about loops, huh? If you want to power a navigation menu by loops, the best way would be using multidimensional arrays and for loop.

你说了关于循环的事?如果你想通过循环来驱动导航菜单,最好的方法是使用多维数组和for循环。

$links = array('Home' => 'index.php', 'login' => 'login.php');
$values = array_values($links);
$keys = array_keys($links);

for($i=0; $i<count($links); $i++) {
   print('<a href="' . $values[$i] . '" class="white">' . $keys[$i] . '</a> | ');
}

foreach example(a lot easier though):

对于每个例子(虽然简单得多):

$links = array('Home' => 'index.php', 'login' => 'login.php');

foreach($links as $val => $link) {
   print('<a href="' . $link . '" class="white">' . $val . '</a> | ');
}