使用外部JS变量更改HTML按钮的文本

时间:2017-07-11 21:31:34

标签: javascript jquery html

在我的主HTML文件中,我使用以下脚本创建变量:

<Grid x:Name="ContentArea"
          Margin="{StaticResource MediumLeftRightMargin}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <CommandBar x:Name="MyAppBar"
                    Grid.Row="0" />

        <Grid Grid.Row="1" Background="DarkGray">
            <TextBlock Text="This is where your page content would go" 
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"/>
        </Grid>

        <!--  Adaptive triggers  -->
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="WindowStates">
                <VisualState x:Name="WideState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="640" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="MyAppBar.(Grid.Row)"
                                Value="0" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="NarrowState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="MyAppBar.(Grid.Row)"
                                Value="2" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>

稍后在我的HTML中,我使用以下代码在div中创建了一个按钮:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
//Creation of answerjson not shown
var choiceOne = answerjson[0].choice;

}); 

如何将choiceOne的值显示为按钮b1上的标签?我在网上搜索过,但只有当按钮不在div中时才能找到这个问题的答案。

3 个答案:

答案 0 :(得分:2)

&#13;
&#13;
var choiceOne = "Some Text";

// jQuery 
$("#b1").text(choiceOne);

// Regular JavaScript
document.getElementById("b2").innerHTML = choiceOne;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="one"><button class="button" id="b1"></button></div>
<div id="two"><button class="button" id="b2"></button></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

像这样:

    public static void generateForest(){
        int[][] forest = new int[50][50];
        Random ran = new Random();
        double randBounds = 150;
        double incr1 = 1.05;
        double incr2 = 1.07;
         for (int i = 0;i<50;i++){
            for(int q = 0;q<50;q++){
                try {
                    double ranNum = ran.nextInt(Math.abs((int) Math.round(randBounds)));
                    if (Math.round(ranNum)==0){
                        forest[i][q] = 0;
                    }
                    else {forest[i][q] = 1;}
                }
                catch(java.lang.IllegalArgumentException e){
                    forest[i][q] = 0;
                }
                if (q<=25){
                    randBounds = (randBounds/incr2);
                }
                else{randBounds = (randBounds*incr2);}
            }
            if (i <=25){
                randBounds = (randBounds/incr1);
            }
            else{randBounds = (randBounds*incr1);System.out.println(randBounds);}
        }
        printTrees(forest);
    }

但您需要在

中执行此操作
$('#b1').html(choiceOne);

所以你确定所有的DOM都已准备就绪并且已经加载......不久之后

答案 2 :(得分:0)

从javascript内部访问该按钮,并使用以下命令更改其上的文本:

document.getElementById("b1").innerHTML = choiceOne;

这是常规的JS。您还可以使用JQuery来选择容器。

相关问题