gpt4 book ai didi

html - 为什么我的 SVG 元素没有出现? (即使在检查我的网页时)

转载 作者:行者123 更新时间:2023-12-04 03:21:44 27 4
gpt4 key购买 nike

所以我尝试在我的下拉菜单中包含一个 SVG 元素,并且我包含了 SVG 属性。但是,似乎没有任何浏览器考虑它;即使在检查页面时,我也无法在任何地方找到 SVG 属性。下面是代码示例(注意我使用的是 CSS 的实用程序框架 Tailwind)。

<select class="font-semi bold text-sm appearance-none bg-gray-200 
inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You</option>
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22" height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5"
d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184
1.184-5.04-5.04 5.04-5.04z">
</path>
</g>
</svg>
</select>

我尝试通过将 SVG 元素放在 select 元素之外但仍然放在 span 元素中来更改 SVG 元素的位置。这一次,SVG 元素是可见的。然而 - 自然地 - 它在 select 元素之外,而不是在最右边(即使在检查 span 元素之后,它显然包含“类别”空间。我认为这可能是浏览器问题,所以我尝试了其他那些(我正在使用 Chrome)和 Tailwind playground 上,没有任何变化。有人有解决方案吗?

最佳答案

您将 svg 放在 select 标签中。

一个选择标签只接受两个 child :

  • 选项
  • 优化组

所有其他元素都被忽略

<!--this works-->
<select>
<optgroup label="opt group" />
<option>Option 1</option>
<option>Option 2</option>
</select>

这两个元素在内部呈现。

让我们看一下您的代码:

            <span>
<select class="font-semi bold text-sm appearance-none bg-gray-200 inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You </option>
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22"
height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg>
</select>

svg 在选择内。

把它搬出去!

<span>
<select class="font-semi bold text-sm appearance-none bg-gray-200 inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You </option>

</select>
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22"
height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg>

用于制作 <右边:

svg {
position: absolute;
right:0;
}

It works when the select is on the top:

svg {
position: absolute;
right: 0;
}
<span>
<select class="font-semi bold text-sm appearance-none bg-gray-200 inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You </option>

</select>
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22"
height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg>

The left facing triangle is on the extreme right of the page, with the CSS above.

如果你有上面的东西,看看会发生什么!它有效:

svg {
position: absolute;
right: 0;
}
<div><strong>this is bold</strong> iqjr 98qc0 v0q89ure qoicqje oqircjoe iaewc r</div>
<span>
<select class="font-semi bold text-sm appearance-none bg-gray-200 inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You </option>

</select>
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22"
height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg>

这也有效:

table {
width: 100%;
}
#triangle {
text-align:right;
}
<div><strong>this is bold</strong> ercjieo aceijrop ioejcrpa aeijrcop aeijopcr aeiojcr aeiojr aij eirojtper ieorjc</div>
<table>
<tr><td><select class="font-semi bold text-sm appearance-none bg-gray-200 inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You </option>

</select>

</td>
<td id="triangle">
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22"
height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg>
</td></tr></table>

但不要使用表格标签。它们带有语义!

表格应该携带数据:

<表类="s-表"><头>col1col2<正文>数据更多数据数据更多数据数据更多数据

但如果它没有携带数据怎么办?

这对屏幕阅读器不利。

所以使用display:table;相反。

.faketable {
display:table;
}
.faketable>div {
display:table-row;
}
.faketable>div>div {
display:table-cell;
padding:7px;
}
<div class="faketable">
<div>
<div>Cell One (aruc b)</div>
<div>Cell Two (ierjc eijac)</div>
</div>
<div>
<div>Cell Three (qrc a)</div>
<div>Cell Four (caf vfjj)</div>
</div>
</div>

所以第二种方法应该更像这样:

.faketable {
width: 100%;
}

#triangle {
text-align: right;
}

.faketable {
display: table;
}

.faketable>div {
display: table-row;
}

.faketable>div>div {
display: table-cell;
padding: 7px;
}
<div><strong>this is bold</strong> ercjieo aceijrop ioejcrpa aeijrcop aeijopcr aeiojcr aeiojr aij eirojtper ieorjc</div>
<div class="faketable">
<div>
<div>
<select class="font-semi bold text-sm appearance-none bg-gray-200 inline-block p-5 px-5 py-2 rounded-2xl ">
<option value="Categor" disabled selected>Category</option>
<option value="personal">Personal</option>
<option value="buisness">Buisness</option>
<option value="You">You </option>

</select>

</div>
<div id="triangle">
<svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22" height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg>
</div>
</div>
</div>

如果你想在 select 标签中使用图片,你可以使用自定义下拉菜单:

var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = document.getElementById('content');
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}

function hide(element) {
var x = document.getElementsByClassName("options");
var i;
for (i = 0; i < x.length; i++) {
x[i].style.backgroundColor = "white";
}
document.getElementById('content').style.display = 'none';
element.style.backgroundColor = "#00ffff"
}
.options:hover {
background-color: #00dddd !important;
}

#buttons,
.options {
cursor: pointer;
}

.options {
border: 1px solid black;
background-color: white;
}

#content {
position: fixed;
top: 1em;
}

svg {
position: absolute;
right: 0;
}
<table>
<tr>
<td><button style="width: 100%;" id="buttons" class="collapsible">Click Me</button> <svg class="transform -rotate-90 absolute pointer-events-none inline flex" style="right: 12px;" width="22" height="22" viewBox="0 0 22 22">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
</path>
<path fill="#222"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
</g>
</svg> </td>
</tr>
<tr>
<td id="content" style="display:none;" onblur="this.style.display='none';">
<ul style="list-style:none;">
<li onclick="hide(this); document.getElementById('buttons').innerHTML=this.innerHTML;" class="options">option 1</li>
<li onclick="hide(this); document.getElementById('buttons').innerHTML=this.innerHTML;" class="options">option 2</li>
<li onclick="hide(this); document.getElementById('buttons').innerHTML=this.innerHTML;" class="options">option 3</li>
</ul>
</td>
</tr>
</table>

关于html - 为什么我的 SVG 元素没有出现? (即使在检查我的网页时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68370059/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com