首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取链接名称并将其记录在div中

获取链接名称并将其记录在div中
EN

Stack Overflow用户
提问于 2013-05-22 07:32:19
回答 2查看 158关注 0票数 0

我希望使用链接中的名称,该链接被单击以显示在div中。我试了很多方法,但什么也没做。我还有一个小脚本来显示和隐藏一个div,在我尝试为该名称创建一个脚本之前,它工作得很好。有没有人能快速看一眼。

代码语言:javascript
复制
//script for hiding and display div
function showDiv() {
   document.getElementById('popup').style.display = "block";
}
function hideDiv() {
   document.getElementById('popup').style.display = "none";
}

和html:

代码语言:javascript
复制
<div class="maintab">
    <div class="tab" onclick="showDiv()" ><a href="#">Template</a></div>
</div>
<div id="bestelknop">**Here the name must come**</div>
<div id="popup" style="display:none">
    <ul>
        <a href="http://pixelweb.be" target="iframe" onclick="hideDiv()" name="Pixelweb">pixelweb</a>
        <a href="http://templates.pixelweb.be/social" target="iframe" onclick="hideDiv()" name="Social">social</a>
        <a href="http://templates.pixelweb.be" target="iframe" onclick="hideDiv()" name="Templates" >templates pixelweb</a>
    </ul>
</div>

现在我想要在div want旋钮中显示name van de current链接。

我是javascript中的newbee,所以请帮我解决这个问题。

致以敬意,

本尼

EN

回答 2

Stack Overflow用户

发布于 2013-05-22 07:53:29

您可以将当前链接传递给函数。

看一下我这里的工作示例:http://jsfiddle.net/XfW3P/

代码语言:javascript
复制
<div class="maintab">
    <div class="tab" onclick="showDiv()" ><a href="#">Template</a></div>
</div>
<div id="bestelknop">**Here the name must come**</div>
<div id="popup" style="display:none">
    <ul>
        <a href="http://pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Pixelweb">pixelweb</a>
        <a href="http://templates.pixelweb.be/social" target="iframe" onclick="hideDiv(this)" name="Social">social</a>
        <a href="http://templates.pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Templates" >templates pixelweb</a>
    </ul>
</div>

JavaScript代码:

代码语言:javascript
复制
function showDiv() {
    document.getElementById('popup').style.display = "block";
}
function hideDiv(link) {
    document.getElementById('popup').style.display = "none";
    document.getElementById('bestelknop').innerHTML = link.name;
}
票数 2
EN

Stack Overflow用户

发布于 2013-05-22 07:53:45

首先,在每个链接中将this传入您的`onclick="hideDiv(this)“:

代码语言:javascript
复制
<a href="http://pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Pixelweb">pixelweb</a>
<a href="http://templates.pixelweb.be/social" target="iframe" onclick="hideDiv(this)" name="Social">social</a>
<a href="http://templates.pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Templates" >templates pixelweb</a>

然后,更改您的hideDiv()函数:

代码语言:javascript
复制
function hideDiv(obj) {
    document.getElementById('popup').style.display = "none";
    document.getElementById('bestelknop').innerHTML = obj.name + " " + obj.href;
}

小提琴:http://jsfiddle.net/bUVtA/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16681194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档