简体   繁体   English

在悬停时使用XML内容更新DIV

[英]Updating DIV with XML content on hover

The concept of what I'm trying to do is fairly simple. 我想要做的事情的概念相当简单。 I have a grid of company logos loaded through XSLT from an XML document, each with their own unique links to the company profiles. 我有一个通过XSLT从XML文档加载的公司徽标网格,每个徽标都有自己唯一的公司配置文件链接。

I have a separate div on the page, essentially a "preview" box. 我在页面上有一个单独的div,本质上是一个“预览”框。 What I want to do is this: 我想要做的是:

I roll over a logo, and it loads the name of the company and a short description into the preview div. 我滚动一个徽标,它将公司名称和简短描述加载到预览div中。 This content is loaded through XML. 此内容通过XML加载。

I have been messing around with the Jquery load() function, changing the target document to load on Hover—and it almost gets what I want, but it loads the whole target XML document into the div. 我一直在搞乱Jquery load()函数,将目标文档更改为在Hover上加载 - 它几乎得到了我想要的东西,但它将整个目标XML文档加载到div中。

How can I separate this target XML data into separate divs? 如何将此目标XML数据分成单独的div? (which I have styled differently) I'd assume I would make use of Ajax in some way. (我的风格不同)我假设我会以某种方式使用Ajax。 I want to load the <name> node into the name_div, and the <desc> node into the description_div, and have them update on hover. 我想将<name>节点加载到name_div中,将<desc>节点加载到description_div中,并在hover上更新它们。 Thanks in advance for the help! 在此先感谢您的帮助!

Here are some examples of the code I'm working with: 以下是我正在使用的代码的一些示例:

portfolio.xml file (this file is my main page, displaying the grid and preview div, example uses 3 companies): portfolio.xml文件(此文件是我的主页面,显示网格和预览div,示例使用3家公司):

<portfolio>
    <company>
        <name>ABC Company</name>
        <sdesc>Consumer products</sdesc>
        <logo-thumb>abcco.jpg</logo-thumb>
        <link>abcco.xml</link>
    </company>

    <company>
        <name>DEF Company</name>
        <sdesc>Communications firm</sdesc>
        <logo-thumb>defco.jpg</logo-thumb>
        <link>defco.xml</link>
    </company>
    <company>
        <name>GHI Corporation</name>
        <sdesc>Electronic products</sdesc>
        <logo-thumb>ghico.jpg</logo-thumb>
        <link>ghico.xml</link>
    </company>
</portfolio>

The following XSLT displays that code on the page: 以下XSLT在页面上显示该代码:

<xsl:for-each select="portfolio/company">
     <xsl:sort select="name" />

       <div class="invest-port-thumb">
         <a>
         <xsl:attribute name="href">
              <xsl:value-of select="link" />
         </xsl:attribute>

         <img>
         <xsl:attribute name="src">
              <xsl:value-of select="logo-thumb" />
         </xsl:attribute>
         </img>
         </a>
       </div>

</xsl:for-each>

This is the HTML structure of the "preview div": 这是“预览div”的HTML结构:

<div id="preview">

<div id="preview-name"> [Name to display here] </div>
<div id="preview-desc"> [Description to display here] </div>

</div>

All 3 company logos are loaded into the page, each displaying a linked image loaded from . 所有3个公司徽标都被加载到页面中,每个徽标都显示从中加载的链接图像。 Desired effect is to on hover, display the contents of in the "preview-name" div, and the contents of in the "preview-desc" div. 期望的效果是悬停,显示“preview-name”div中的内容,以及“preview-desc”div中的内容。


Let me try to be a bit more specific. 让我试着更具体一点。 Here are some examples: 这里有些例子:

portfolio.xml file (this file is my main page, displaying the grid and preview div, example uses 3 companies): portfolio.xml文件(此文件是我的主页面,显示网格和预览div,示例使用3家公司):

<portfolio>
    <company>
        <name>ABC Company</name>
        <sdesc>Consumer products</sdesc>
        <logo-thumb>abcco.jpg</logo-thumb>
        <link>abcco.xml</link>
    </company>

    <company>
        <name>DEF Company</name>
        <sdesc>Communications firm</sdesc>
        <logo-thumb>defco.jpg</logo-thumb>
        <link>defco.xml</link>
    </company>
    <company>
        <name>GHI Corporation</name>
        <sdesc>Electronic products</sdesc>
        <logo-thumb>ghico.jpg</logo-thumb>
        <link>ghico.xml</link>
    </company>
</portfolio>

The following XSLT displays that code on the page: 以下XSLT在页面上显示该代码:

<xsl:for-each select="portfolio/company">
     <xsl:sort select="name" />

       <div class="invest-port-thumb">
         <a>
         <xsl:attribute name="href">
              <xsl:value-of select="link" />
         </xsl:attribute>

         <img>
         <xsl:attribute name="src">
              <xsl:value-of select="logo-thumb" />
         </xsl:attribute>
         </img>
         </a>
       </div>

</xsl:for-each>

This is the HTML structure of the "preview div": 这是“预览div”的HTML结构:

<div id="preview">

<div id="preview-name"> [Name to display here] </div>
<div id="preview-desc"> [Description to display here] </div>

</div>

All 3 company logos are loaded into the page, each displaying a linked image loaded from <logo-thumb> . 所有3个公司徽标都被加载到页面中,每个徽标都显示从<logo-thumb>加载的链接图像。 Desired effect is to on hover, display the contents of <name> in the "preview-name" div, and the contents of <sdesc> in the "preview-desc" div. 期望的效果是在悬停时,在“preview-name”div中显示<name>的内容,在“preview-desc”div中显示<sdesc>的内容。

Preserving the semantics of your page and the structure of your stylesheet, I try to do something like this: 保留页面的语义和样式表的结构,我尝试做这样的事情:

<xsl:for-each select="portfolio/company"> 
     <xsl:sort select="name" /> 
     <div class="invest-port-thumb"> 
       <a href="{link}"><img src="{logo-thumb}" /></a>
       <div class="preview"> 
         <div class="preview-name"><xsl:value-of select="name" /></div> 
         <div class="preview-desc"><xsl:value-of select="sdesc" /></div> 
       </div> 
     </div> 
</xsl:for-each>

Then, in CSS stylesheet, you can use :hover pseudoclass and + combinator to show or hide preview. 然后,在CSS样式表中,您可以使用:hover pseudoclass和+ combinator来显示或隐藏预览。 There ara some problems with crossbrowser compatibility. 有一些问题与crossbrowser兼容性。 You should check Stu Nicholls site for better semantics and CSS examples. 您应该检查Stu Nicholls站点以获得更好的语义和CSS示例。

Strictly speaking, you load() can take a selector after the url and only return a part of the ajax call. 严格来说, load()可以在url之后选择一个选择器,只返回ajax调用的一部分。

So, something like: 所以,像:

$('#name_div').load('/path/yourXmlFile.xml name:first');

Now, making network calls every time you want to look at something is not very efficient. 现在,每次想要查看某些内容时进行网络调用都不是很有效。 You probably want to cache the output into a variable. 您可能希望将输出缓存到变量中。

I would feed the name and desc into the anchor as part of the anchor or it's child image, perhaps using the ALT tag for the name and REL tag for the description. 我会将名称和desc作为锚点或其子图像的一部分提供给锚点,可能使用ALT标记作为名称,REL标记作为描述。

On hover over any anchor, I'll use jQuery [.attr()] to grab the attribute values containing the name and description you want and spit that into your respective target DIVs. 在将鼠标悬停在任何锚点上时,我将使用jQuery [.attr()]来获取包含所需名称和描述的属性值,并将其吐入各自的目标DIV中。

(See 'Recipe Categories' in http://www.masterchef.com.au/home.htm for similar example) (有关类似示例,请参阅http://www.masterchef.com.au/home.htm中的 “食谱类别”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM