简体   繁体   English

在跨度中显示选择文本

[英]Displaying selection text in span

So let's say I have this code: 所以说我有这段代码:

<span id="select_list">
    <ul>
        <li><a id="1">1</a></li>
        <li><a id="2">2</a></li>
        <li><a id="3">3</a></li>
    </ul>
</span>
<span id="selection"></span>

And let's also assume that there are a lot of list elements, ex. 并且我们还假设有很多列表元素,例如。 '4,5,6,7... etc'. '4,5,6,7 ...等'。 Can I get a html file, that is basically just text, that corresponds to the list element's ID (ex. 1.html, 2.html,... etc), to show in 'selection'? 我可以获取一个HTML文件,该文件基本上只是文本,与列表元素的ID(例如1.html,2.html等)相对应,以“选择”显示吗? If so how? 如果可以,怎么办? Thanks for your time. 谢谢你的时间。 Hope I explained it well. 希望我能很好地解释。

Something like this (jQuery) should work: 像这样的东西(jQuery)应该工作:

var list = $("#select_list");
var sel = $("#selection");

$("a", list).on("click", function(){
    var id = $(this).attr("id");
    sel.load(id+".html");
}); 
<div id="select_list">
    <ul>
      <li id="1"><a href="#">1</a></li>
      <li id="2"><a href="#">2</a></li>
      <li id="3"><a href="#">3</a></li>
</ul>
</div>
<div id="selection"></div>

i would use a div not span spans are for if you want to change the size of something particular like this: 如果您想更改某些特定内容的大小,我会使用div而不是span来做:

  <li id="1" href="#"><a href="#"><span style="color: red; 
  font-size: 30px">1</span></a></li>

and from what i am understanding you want a selector to select them in css? 从我的理解,你想要一个选择器在CSS中选择它们?

if so this is how: 如果是这样,这是如何的:

 #select_list ul li:nth_child(1) {

 }

or 要么

#select_list ul li#2 {

 }

hope this helps you 希望这对您有帮助

I would suggest using data-attributes instead of IDs. 我建议使用数据属性而不是ID。

HTML 的HTML

<ul class='selection-list'>
    <li data-name='Dog'>Dog</li>
    <li data-name='cat.html'>Cat</li>
    <li data-name='45'>Fourty Five</li>
    <li data-name='Triangle'>Three sides</li>
</ul>

<div class="output js-output"></div>

jQuery jQuery的

var $output = $('.js-output');

$('.selection-list li').on('click', function() {
    var selectionValue = $(this).data('name');
    $output.text(selectionValue);
});

CSS 的CSS

.selection-list li {
    cursor: pointer;
}

jsFiddle jsFiddle

iframe iframe

I'm starting to think that you are asking for an iframe with dynamic source. 我开始认为您正在请求具有动态源的iframe。 The question is unclear. 问题尚不清楚。 You may want to try and rewrite it. 您可能要尝试重写它。 - Here is what I think you may be after... -我认为这可能是您追求的...

HTML 的HTML

<ul class='selection-list'>
    <li data-url='http://reputable.agency'>Reputable Agency</li>
    <li data-url='http://perpetual.education'>Perpetual Education</li>
    <li data-url='http://example.com/index.html'>Example.com</li>
</ul>

<iframe src='http://example.com' class="output js-output"></iframe>

JavaScript / jQuery JavaScript / jQuery

var $output = $('.js-output');

$('.selection-list li').on('click', function() {
    // get the 'data-url' from the element...
    var selectionValue = $(this).data('url');
    // put that data-url into the src attribute of the iFrame
    $output.attr('src', selectionValue);
});

Also.. 也..

Note that if you are using the same domain for all of these, you can build those urls differently to keep things simple. 请注意,如果所有这些都使用相同的域,则可以以不同的方式构建这些URL,以使事情简单。

<li data-url='index.html'>Example.com</li>

$output.attr('src', 'http://yoursite.com/' + selectionValue);

jsFiddle jsFiddle


AJAX AJAX

Now I'm wondering if you mean AJAX. 现在,我想知道您是否要使用AJAX。 Here is an example - but it's not tested because I don't have access to a bunch of relative URLs - but here is the basics - and should lead you to the right documentation. 这是一个示例-但未经测试,因为我无权访问大量相对URL-但这里是基础知识-应该引导您找到正确的文档。

HTML 的HTML

<ul class='selection-list'>
    <li data-url='index.html'>Reputable Agency</li>
    <li data-url='index.html'>Perpetual Education</li>
    <li data-url='index.html'>Example.com</li>
</ul>

<div class="output js-output"></div>

JavaScript / jQuery JavaScript / jQuery

var $output = $('.js-output');

var getOtherPage = function(target) {
    $.ajax({
       url: target,
       success:function(response){
          $output.html(response);
       },error:function(){
          alert("error");
       }
    });
};

$('.selection-list li').on('click', function() {
    var selectionValue = $(this).data('url');
    getOtherPage(selectionValue);
});

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

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