在链接上显示网页内的文本

发布于 2025-01-04 22:56:34 字数 255 浏览 1 评论 0原文

有没有一种简单的方法可以使用链接显示网页中的文本,而无需更改网址或使用 iframe?也许有一个“onclick”功能?我对新代码很陌生,所以不知道从哪里开始。我附上了一张我之后的照片,相当简单。我已经使用 iframe 作为主界面,因此另一个 iframe 的默认菜单会变得混乱。必须有一个简单的修复..任何帮助将非常感激。 谢谢,亚伦

在此处输入图像描述

is there a simple way to reveal text within a webpage using a link without altering the web address or using an iframe? maybe with an 'onclick' function? im pretty new to new code so not sure where to start.. ive attached a picture of what exaclty im after, fairly simple. im already using an iframe as the main interface so another one would get messy in terms of a default menu. there must be a simple fix.. any help would be really appreciated.
thanks, Aaron

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2025-01-11 22:56:34

将要隐藏的文本放入隐藏容器内,直到单击为止,如下所示:

<div id="HiddenTextContainer" style="display: none;">
   Hello, I will become visible when you click something else
</div>

下一步是将 JavaScript 代码添加到页面,例如在 部分内:

function ShowHiddenText() {
    document.getElementById("HiddenTextContainer").style.display = "block";
}

最后有这样的代码:

<span onclick="ShowHiddenText();">click me to show hidden text</span>

实时测试用例

编辑:如果要显示多个元素,可以使用 rel 属性:

<span rel="HiddenTextContainer2">click me to show second hidden text</span><br />

然后使用纯 JavaScript 迭代具有该属性的所有元素并分配它们的 onclick 以编程方式:

window.onload = function() {
    var elements = document.getElementsByTagName("span");
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        var id = element.getAttribute("rel") || "";
        if (id.length > 0) {
            element.onclick = function() {
                var oToShow = document.getElementById(this.getAttribute("rel"));
                if (oToShow)
                    oToShow.style.display = "block";
            };
        }
    }
};

单击时,将显示 ID 与 rel 值相同的元素。

更新了小提琴

编辑:要在一个容器中显示它,首先要有这样的容器:

<div id="HiddenTextContainer"></div>

不需要将其隐藏,因为它最初是空的,然后将代码更改为:

window.onload = function() {
    var elements = document.getElementsByTagName("span");
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        var id = element.getAttribute("rel") || "";
        if (id.length > 0) {
            element.onclick = function() {
                var oToShow = document.getElementById(this.getAttribute("rel"));
                if (oToShow)
                    document.getElementById("HiddenTextContainer").innerHTML = oToShow.innerHTML;
            };
        }
    }
};

复制其内容,而不是显示相关容器到“主”容器。

更新了 jsFiddle

Put the text you want to hide until click inside hidden container, like this:

<div id="HiddenTextContainer" style="display: none;">
   Hello, I will become visible when you click something else
</div>

Next step is add that JavaScript code to the page, for example inside the <head> section:

function ShowHiddenText() {
    document.getElementById("HiddenTextContainer").style.display = "block";
}

And finally have such code:

<span onclick="ShowHiddenText();">click me to show hidden text</span>

Live test case.

Edit: in case you got more than one element to show, you can use the rel attribute:

<span rel="HiddenTextContainer2">click me to show second hidden text</span><br />

Then with pure JavaScript iterate over all elements with that attribute and assign their onclick programmatically:

window.onload = function() {
    var elements = document.getElementsByTagName("span");
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        var id = element.getAttribute("rel") || "";
        if (id.length > 0) {
            element.onclick = function() {
                var oToShow = document.getElementById(this.getAttribute("rel"));
                if (oToShow)
                    oToShow.style.display = "block";
            };
        }
    }
};

When clicked, element with ID the same as the rel value will be displayed.

Updated fiddle.

Edit: to show it in one single container, first have such container:

<div id="HiddenTextContainer"></div>

No need to have it hidden since it's initially empty, then change the code to:

window.onload = function() {
    var elements = document.getElementsByTagName("span");
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        var id = element.getAttribute("rel") || "";
        if (id.length > 0) {
            element.onclick = function() {
                var oToShow = document.getElementById(this.getAttribute("rel"));
                if (oToShow)
                    document.getElementById("HiddenTextContainer").innerHTML = oToShow.innerHTML;
            };
        }
    }
};

Instead of showing the related container, you copy its contents to the "main" container.

Updated jsFiddle.

初与友歌 2025-01-11 22:56:34

为此,您有 2 个选择。第一种是预加载页面上的所有内容,然后仅在单击链接时设置可见属性。第二种是使用 AJAX 之类的方式加载它,然后以与上面相同的方式显示它。

要显示这些内容,请查看 JQuery: http://jquery.com/

第二种方法的一个很好的教程在这里: http://yensdesign.com/2008/12/how-to-load-content-via-ajax-in-jquery/

You have 2 choices for this. The first is to preload everything on the page and then only set the visible property when you click the link. The second is to load it in using something like AJAX and then show it the same way as above.

To show these things look into JQuery: http://jquery.com/

A good tutorial for the second method is here: http://yensdesign.com/2008/12/how-to-load-content-via-ajax-in-jquery/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文