0
我有一個工具提示,我使用的是JS和CSS 但是,當它在<li>
中時,它顯示在頁面左側,而不是圖像旁邊,就像它應該那樣。工具提示css在li中的位置絕對
任何想法,它不工作在<li>
。 下面是代碼
JS代碼
this.showTooltip = function (a, b) {
$("#" + b).css("left", $(a).position().left + 20 + "px");
$("#" + b).css("top", $(a).position().top + "px");
$("#" + b).fadeIn("slow")
};
this.hideTooltip = function (a) {
$("#" + a).fadeOut("slow")
};
this.formSubmit = function (a) {
$("#orderform")[0].submit()
};
this.mainPageShow = function (b, a) {
$("#header_" + b).hide();
$("#header_" + a).show()
}
CSS代碼
.tooltip {
position: absolute;
z-index: 100;
padding: 10px;
border: solid 1px #dadada;
background-color: #f9f9f9;
width: 200px;
}
HTML代碼
<ul>
<li><img onmouseover="showTooltip(this,'a');" onmouseout="hideTooltip('a');" alt="Tooltip" src="img/content/question.gif" width="20" height="20" align="left" />A</li>
<li><img onmouseover="showTooltip(this,'b');" onmouseout="hideTooltip('b');" alt="Tooltip" src="img/content/question.gif" width="20" height="20" align="left" />B</li>
<li><img onmouseover="showTooltip(this,'c');" onmouseout="hideTooltip('c');" alt="Tooltip" src="img/content/question.gif" width="20" height="20" align="left" />C</li>
</ul>
<div class="tooltip" id="a" style="display: none;">A test</div>
<div class="tooltip" id="b" style="display: none;">B test</div>
<div class="tooltip" id="c" style="display: none;">C test</div>
您知道絕對定位元素相對於['offsetParent'](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.offsetParent)元素的位置?即到最接近的元素,它不是靜態的。 – Teemu
儘管我的回答如下,關於你的具體情況,你應該嘗試使用offset()而不是position()。如果您的元素絕對或相對定位,位置()將只能正常工作,但情況並非如此。 – leandroico
抵消了伎倆,謝謝 – Garry